home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPtxt_20.lha / doc / programmertext.txt < prev    next >
Text File  |  1993-08-13  |  232KB  |  6,867 lines

  1.  
  2.  
  3.  
  4.  
  5. Chapter 3
  6.  
  7.  
  8.  
  9. Programmer's  Manual
  10.  
  11.  
  12.  
  13. This chapter provides an in depth description of the AmiTCP/IP
  14. application programming interface.  Following sectionsintroduce the
  15. socket model of communication (3.1) andthe bsdsocket.library function
  16. calls implementing the socket abstraction.   Some useful supporting
  17. routines are described in section 3.2.  The client/server model is
  18. introduced in section 3.3.  Some more advanced topics are discussed in
  19. section 3.4.  Section 3.5 summarizes the small differences between
  20. AmiTCP/IP and 4.3BSD socket APIs.  The full function reference of the
  21. AmiTCP/IP API functions is in appendix Bstarting from page 128.
  22.    The textin sections 3.1 -- 3.4 is based on the [Leffler et al 1991a ].
  23.  
  24.  
  25.  
  26. 3.1     Socket Concepts
  27.  
  28.  
  29. The basic building block for communication is the socket.  A socket is an
  30. endpoint of communication to which a name may be bound.  Each socket in
  31. use has a type and one or more associated processes.  Sockets exist
  32. within communication domains.  A communication domain is an abstraction
  33. introduced to bundle common properties of processes communicating through
  34. sockets.  One such property is the scheme used to name sockets.  Sockets
  35.  
  36. normally exchange data only with socketsin the same domain1.   The
  37. AmiTCP/IP system supports currently onlyone communication domain:  the
  38. Internet domain, which is used by processes which communicate using the
  39. the DARPA standard communication protocols.   The underlying communication
  40. facilities provided by the domains havea significant influence on the internal
  41. system implementation as well as the interface to socket facilities available
  42. to a user.
  43.  
  44.  
  45.  
  46. 3.1.1   Socket  Types
  47.  
  48. Sockets are typed according to the communication properties visible to a
  49. user.  Processes are presumed to communicate only between sockets of the
  50. same type, although there is nothing that prevents communication between
  51. ________________________________
  52.    1It may be possible to cross domain boundaries, but only if some
  53.  
  54. translation process is performed.
  55.  
  56.  
  57.  
  58.                                       27
  59.  
  60.  
  61. 28    Section 3.1                  AmiTCP/IP                   System Manual
  62.  
  63.  
  64.  
  65. sockets of different types should the underlying communication protocols
  66. support this.
  67.    Three types of sockets currently are available to a user.  A stream
  68. socket provides for the bidirectional, reliable, sequenced, and
  69. unduplicated flow of data without recordboundaries.   Aside from the
  70. bidirectionality of data flow, a pair ofconnected stream sockets
  71.  
  72. provides an interface nearly identical to that of pipes.2
  73.    A datagram socket supports bidirectional flow of data which is not
  74. promised to be sequenced, reliable, or unduplicated.  That is, a process
  75. receiving messages on a datagram socketmay find messages duplicated,
  76. and, possibly, in an order different from the order in which it was sent.
  77. An important characteristic of a datagram socket is that record
  78. boundaries in data are preserved.  Datagram sockets closely model the
  79. facilities found in many contemporary packet switched networks such as
  80. the Ethernet.
  81.    A raw socket provides users access to the underlying communication
  82. protocols which support socket abstractions.   These sockets are normally
  83. datagram oriented, though their exact characteristics are dependent on
  84. the interface provided by the protocol.  Raw sockets are not intended for
  85. the general user; they have been provided mainly for those interested in
  86. developing new communication protocols,or for gaining access to some of
  87. the more esoteric facilities of an existing protocol.  The use of raw
  88. sockets is considered in section 3.4.
  89.    Another potential socket type which has interesting properties is the
  90. reliably delivered message socket.  The reliably delivered message socket
  91. has similar properties to a datagram socket, but with reliable delivery.
  92. There is currently no support for this type of socket, but a reliably
  93. delivered message protocol similar to Xerox's Packet Exchange Protocol
  94. (PEX) may be simulated at the user level.   More information on this topic
  95. can be found in section 3.4.
  96.  
  97.  
  98.  
  99. 3.1.2   Using The Socket Library
  100.  
  101. As any other Amiga shared library the bsdsocket.library must be opened to
  102. be able to access the functions in the library.   Thiscan be done with
  103. Exec's OpenLibrary() call.  The call returns a librarybase pointer which
  104. is task specfic, which means that each separate task (or process) must
  105. open the library itself.  This is because the AmiTCP/IPstores task
  106. specific information to the library basestructure.
  107.    The library base pointer returned by the OpenLibrary() must be stored
  108. in to a variable accessable from the program (usually global) named
  109. SocketBase.  Example of opening the library follows:
  110.  
  111.  
  112. #include <exec/libraries.h>
  113.  ...
  114. struct Library *SocketBase = NULL;
  115.  ...
  116.     if ((SocketBase = OpenLibrary("bsdsocket.library", 2)) == NULL) -
  117. ________________________________
  118.    2In the UNIX systems pipes havebeen implemented internally as simply
  119.  
  120. a pair of connected stream sockets.
  121.  
  122.  
  123. System Manual                   AmiTCP/IP                   Section 3.1    29
  124.  
  125.  
  126.  
  127.          /* couldnot open the library */
  128.           ...
  129.     "
  130.     else -
  131.          /* SocketBase now points to socket base of this task */
  132.           ...
  133.     "
  134.  
  135.  
  136.    Note thatthe library version argument of the OpenLibrary() call is
  137. given as 2, which means that at least version 2 is needed.  This is the
  138. minimum version which should be requested, since the version 1 is
  139. incompatible with the version 2 and up.  If the application uses features
  140. defined for some specific version (and up), a later version number should
  141. be specified.
  142.    After theapplication is done with sockets the library must be closed.
  143. This is done with CloseLibrary() as follows:
  144.  
  145.  
  146.     if (SocketBase) -
  147.          CloseLibrary(SocketBase);
  148.          SocketBase = NULL;
  149.     "
  150.  
  151.  
  152.    Note thatif the application in question is multithreaded, each task
  153. (or process) need to open/close its ownlibrary base.  The base opened by
  154. the net.lib may be used by the originaltask only!
  155.    Many programs expect the error values of the socket calls to be placed
  156. in a global variable named errno.  By default a sharedlibrary cannot
  157. know the address (nor size) of the applications variables, however.
  158. There are two remedies to this:
  159.  
  160.  
  161.  1. Use function Errno() to retrieve the error value, or
  162.  
  163.  
  164.  2. Tell the address and the size of the errno variable to the AmiTCP/IP
  165.     by using the SetErrnoPtr() call.
  166.  
  167.  
  168.    The latter method requires only one additional function call to the
  169. startup of the application, and is thusthe preferred method.  The call
  170. may look like:
  171.  
  172.  
  173. #include <errno.h>
  174. #include <sys/socket.h>
  175.  ...
  176.     SetErrnoPtr(&errno, sizeof(errno));
  177.  
  178.  
  179.    All thisis done automatically for the application if it is linked with
  180.  
  181. the net.lib3.  See section 3.1.3 for more information about the net.lib
  182. and about compiling and linking the applications.
  183. ________________________________
  184.    3The net.lib is compiler dependent and is currently defined for SASC 6
  185.  
  186. only.  The actual name of the library varies and depends on the compiler
  187. options used.
  188.  
  189.  
  190. 30    Section 3.1                  AmiTCP/IP                   System Manual
  191.  
  192.  
  193.  
  194. 3.1.3   Compiling and Linking The Applications
  195.  
  196. AmiTCP/IP provides standard BSD Unix header files to be used by the
  197. applications.  Normally they are installed to a directory which is
  198. assigned to a name NETINCLUDE: (see section 1.1).   This means that you
  199. should add the NETINCLUDE: to the compilers search path for include
  200. files.
  201.    The include files are decribed briefly in the following subsection:
  202.  
  203.  
  204.  
  205. The NETINCLUDE Header Files
  206.  
  207. bsdsocket.h  This file includes compiler specific prototypes and inline
  208.     functions for bsdsocket.library.  Currently supported compilers are
  209.     GCC and SAS C version 6.  The prototypes for the library functions
  210.     are automatically included by the include files when appropriate,
  211.     i.e.  when the prototypes where declared in the original BSD
  212.     includes.  Thus the bsdsocket.h is included by sys/socket.h, netdb.h
  213.     and arpa/inet.h.
  214.  
  215.     For other compilers only C prototypes are included, so stub routines
  216.     should be used to call the functions.
  217.  
  218. errno.h Replacement for the errno.h included in the standard C-compiler
  219.     headers.  This includes the file sys/errno.h, which defines symbolic
  220.     constants for the error values returned by socket library calls.
  221.     This file is BSD compatible and may well replace file provided by the
  222.     SAS/C 6.
  223.  
  224. netdb.h Contains definitions and prototypes for the network database
  225.     functions, such as the gethostbyname().
  226.  
  227.  Standard BSD System Headers
  228.  
  229.  
  230.     sys/errno.h  Error code definitions for system functions.
  231.  
  232.     sys/ioctl.h  Definitions for socket IO control.
  233.  
  234.     sys/param.h  General machine independent parameter definitions.
  235.  
  236.     sys/socket.h  Definitions related to sockets:  types, address
  237.         families, options and prototypes.
  238.  
  239.     sys/syslog.h  Definations for system logging facilities.
  240.  
  241.     sys/time.h  Definition of structure timeval.
  242.  
  243.     sys/types.h  Common C type definitions and file descriptorset macros
  244.         for select().
  245.  
  246.  
  247.  Internet Related Headers
  248.  
  249.  
  250.     arpa/inet.h  Inet library function prototypes (inet_addr() etc.).
  251.         Included for compatibility and only includes other include files.
  252.  
  253.     netinet/in.h  Protocol numbers, port conventions, inet address
  254.         definitions.
  255.  
  256.     netinet/in_systm.h  Some network byte order type definitions.
  257.  
  258.     netinet/ip.h  IP packet header, packet options, timestamp.
  259.  
  260.  
  261. System Manual                   AmiTCP/IP                   Section 3.1    31
  262.  
  263.  
  264.  
  265.     netinet/ip_icmp.h  ICMP packet structure.
  266.  
  267.     netinet/ip_var.h  Defines IP statistics, external IP packet header,
  268.         reassemble queues structures.
  269.  
  270.     netinet/tcp.h  Defines the TCP packet structure.
  271.  
  272.     netinet/udp.h  Defines the UDP packet structure.
  273.  
  274.  
  275.  Network Related Headers
  276.  
  277.     net/if.h  Defines the interface for network adapter drivers.
  278.  
  279.     net/if_arp.h  General protocol independent ARP structures.
  280.  
  281.     net/route.h  Routing ioctl definitions.
  282.  
  283.     net/sana2errno.h  Sana-II related error definitions.
  284.  
  285.     net/sana2tags.h  Tag definitions for configuring the Sana-II software
  286.         network interface.
  287.  
  288.  Inetd Support
  289.  
  290.     inetd.h  Internet daemon interface definitions.
  291.  
  292.     inetdlib.h  Internet daemon library definitions.
  293.  
  294.  Prototypes
  295.  
  296.     clib/socket_inlines.h   Inline function definitions for those BSD
  297.         socket API functions, which are not implemented strictly like
  298.         originals by bsdsocket.library.
  299.  
  300.     clib/socket_protos.h  bsdsocket.library function call prototypes.
  301.  
  302.  SAS/C Pragmas
  303.  
  304.     pragmas/socket_pragmas.h   SAS/C pragma library calls for
  305.         bsdsocket.library.
  306.  
  307.  SAS/C Proto -file
  308.  
  309.     proto/socket.h  Include file normally included by the SAS/C programs.
  310.         Defines the socket base variable and includes the files
  311.         clib/socket_protos.h and pragmas/socket_pragmas.h.
  312.  
  313.  GCC Inline Functions
  314.  
  315.     inline/socket.h  GCC inline functions for the bsdsocket.library
  316.         functions.
  317.  
  318.  Function Description File
  319.  
  320.     fd/socket_lib.fd  Standard fd-file which specifies in which registers
  321.         the arguments to the bsdsocket.library functions are passed.
  322.         This file can be used to obtain information needed to call the
  323.         bsdsocket.library functions by the assembler programs.
  324.  
  325.  Sana-II Header Files
  326.  
  327.     devices/sana2.h  Definitions for the Sana-II network device driver
  328.         interface.
  329.  
  330.  
  331. 32    Section 3.1                  AmiTCP/IP                   System Manual
  332.  
  333.  
  334.  
  335.     devices/sana2specialstats.h   Special statistics definitions for the
  336.         Sana-II.
  337.  
  338.  Miscellaneous
  339.  
  340.     charread.h  Macro package to do buffered byte-by-byte reading from a
  341.         socket.
  342.  
  343.     lineread.h  Definitions for buffered line orientedreading from a
  344.         socket.
  345.  
  346.  
  347.  
  348. Linking With net.lib
  349.  
  350. AmiTCP/IP distribution includes a link library named net.lib to be used
  351. by the applications.  It is normally located in the directory which has
  352. an assigned name NETLIB:.
  353.    The library contains compiler dependent code which makes the library
  354.  
  355. itself compiler dependent.  Currently only SASC version6 is supported4.
  356.    net.lib features automatic initialization and termination functions
  357. which open and close the bsdsocket.library for the application.  Using
  358. this feature it is possible to compile some typical BSD Unix socket based
  359. applications with AmiTCP/IP without anymodifications to the original
  360. source code.  Note that this base may be used by the process starting the
  361. program, i.e.  the one that executes themain().  This applies to the
  362. included utility functions which call the socket library, too.
  363.    The library also defines new array of error names to be used by
  364. perror() library function.  This is done because the error name array
  365. normally used by Amiga C compilers doesnot contain enough error entries,
  366. resulting perror() to print "Unknown error code" if some socket error is
  367. passed.  Note that for perror() to work the error valuemust be placed
  368. into the global errno variable.  This is accomplished by the
  369. SetErrnoPrt() call made in the automaticinitialization function.
  370.    For the library functions to take effect, the library must be specified
  371. before the C compiler own libraries in the link command line.
  372.  
  373.  
  374.  
  375. 3.1.4   Socket  Creation
  376.  
  377. To create a socket the socket() system call is used:
  378.  
  379.     s = socket(domain, type, protocol);
  380.  
  381. This call requests that the system create a socket in the specified
  382. domain and of the specified type.  A particular protocol may also be
  383. requested.  If the protocol is left unspecified (a value of 0), the
  384. system will select an appropriate protocol from those protocols which
  385. comprise the communication domain and which may be used to support the
  386. requested socket type.  The user is returned a descriptor (a small
  387. integer number) which may be used in later system calls which operate on
  388. sockets.  The domain is specified as one of the manifest constants
  389. ________________________________
  390.    4But since the source for the library is provided, it can be used with
  391.  
  392. any C compiler.
  393.  
  394.  
  395. System Manual                   AmiTCP/IP                   Section 3.1    33
  396.  
  397.  
  398.  
  399. defined in the file sys/socket.h.  For the Internet domain the constant
  400.  
  401. is AF_INET5. The socket types are also defined in this file and one of
  402. SOCK_STREAM, SOCK_DGRAM or SOCK_RAW mustbe specified.  To create a stream socket
  403. in the Internet domain the following call might be used:
  404.  
  405.     s = socket(AF_INET, SOCK_STREAM, 0);
  406.  
  407. This call would result in a stream socket being created with the TCP
  408. protocol providing the underlying communication support.  To create a
  409. datagram socket the call might be:
  410.  
  411.     s = socket(AF_INET, SOCK_DGRAM, 0);
  412.  
  413.    The default protocol (used when the protocol argument to the socket()
  414. call is 0) should be correct for most every situation.  However, it is
  415. possible to specify a protocol other than the default; this will be
  416. covered in section 3.4.
  417.    There areseveral reasons a socket() call may fail.  Aside from the
  418. rare occurrence of lack of memory (ENOBUFS), a socket request may fail
  419. due to a request for an unknown protocol(EPROTONOSUPPORT), or a request
  420. for a type of socket for which there isno supporting protocol
  421. (EPROTOTYPE).
  422.  
  423.  
  424.  
  425. 3.1.5   Binding Local Names
  426.  
  427. A socket is created without a name.  Until a name is bound to a socket,
  428. processes have no way to reference it and, consequently, no messages may
  429. be received on it.  Communicating processes are bound by an association.
  430. In the Internet domain, an association is composed of local and foreign
  431. addresses, and local and foreign ports,In most domains, associations
  432. must be unique.  In the Internet domain there may neverbe duplicate
  433. <protocol, local address, local port, foreign address, foreign port>
  434. tuples.
  435.    The bind() system call allows a process to specify half of an
  436. association, <local address, local port>, while the connect() and
  437. accept() calls are used to complete a socket's association.
  438.    In the Internet domain, binding names to sockets can be fairly complex.
  439. Fortunately, it is usually not necessaryto specifically bind an address
  440. and port number to a socket, because theconnect() and send() calls will
  441. automatically bind an appropriate address if they are used with an
  442. unbound socket.
  443.    The bind() system call is used as follows:
  444.  
  445.     bind(s, name, namelen);
  446.  
  447. The bound name is a variable length bytestring which is interpreted by
  448. the supporting protocol(s).  Its interpretation may vary from
  449. communication domain to communication domain (this is one of the
  450. properties which comprise the domain).  As mentioned, in the Internet
  451. domain names contain an Internet addressand port number.
  452.    In binding an Internet address things are a little complicated:
  453. ________________________________
  454.    5The manifest constants are named AF _whatever as they indicate the
  455.  
  456. ``address format'' to use in interpreting names.
  457.  
  458.  
  459. 34    Section 3.1                  AmiTCP/IP                   System Manual
  460.  
  461.  
  462.  
  463.     #include <sys/types.h>
  464.     #include <netinet/in.h>
  465.      ...
  466.     struct sockaddr_in sin;
  467.      ...
  468.     bind(s, (struct sockaddr *) &sin, sizeof (sin));
  469.  
  470. The selection of what to place in the address sin requires some
  471. discussion.  We will come back to the problem of formulating Internet
  472. addresses in section 3.2 when the library routines used in name
  473. resolution are discussed.
  474.  
  475.  
  476.  
  477. 3.1.6   Connection Establishment
  478.  
  479. Connection establishment is asymmetric,with one process a ``client'' and
  480. the other a ``server''.  The server, when willing to offer its advertised
  481. services, binds a socket to a well--known address associated with the
  482. service and then passively ``listens'' on its socket.  It is then
  483. possible for an unrelated process to rendezvous with the server.  The
  484. client requests services from the serverby initiating a ``connection''
  485. to the server's socket.  On the client side the connect() call is used to
  486. initiate a connection.  Using the Internet domain, thismight apper as:
  487.  
  488.     struct sockaddr_in server;
  489.      ...
  490.     connect(s, (struct sockaddr *)&server, sizeof (server));
  491.  
  492.    where server in the example above would contain Internet address and
  493. port number of the server to which the client process wishes to speak.
  494. If the client process's socket is unbound at the time of the connect
  495. call, the system will automatically select and bind a name to the socket
  496. if necessary.  This is the usual way that local addresses are bound to a
  497. socket.
  498.    An erroris returned if the connection was unsuccessful (any name
  499. automatically bound by the system, however, remains).  Otherwise, the
  500. socket is associated with the server anddata transfer may begin.  Some
  501. of the more common errors returned whena connection attempt fails are:
  502.  
  503.  
  504.  ETIMEDOUT After failing to establish a connection for a period of time,
  505.     the system decided there was no point in retrying the connection
  506.     attempt any more.  This usually occurs because the destination host
  507.     is down, or because problems in the network resulted in transmissions
  508.     being lost.
  509.  
  510.  ECONNREFUSED The host refusedservice for some reason.  This is usually
  511.     due to a server process not being present at the requested name.
  512.  
  513.  ENETDOWN or EHOSTDOWN These operational errors are returned based on
  514.     status information delivered to the client host by the underlying
  515.     communication services.
  516.  
  517.  ENETUNREACH or EHOSTUNREACH These operational errors can occur either
  518.     because the network or host is unknown (no route to the network or
  519.  
  520.  
  521. System Manual                   AmiTCP/IP                   Section 3.1    35
  522.  
  523.  
  524.  
  525.     host is present), or because of status information returned by
  526.     intermediate gateways or switching nodes.  Many times the status
  527.     returned is not sufficient to distinguish a network being down from a
  528.     host being down, in which case the system indicates the entire
  529.     network is unreachable.
  530.  
  531.  
  532.    For the server to receive a client's connection it must perform two
  533. steps after binding its socket.  The first is to indicate a willingness
  534. to listen for incoming connection requests:
  535.  
  536.  
  537.     listen(s, 5);
  538.  
  539.  
  540. The second parameter to the listen() call specifies the maximum number of
  541. outstanding connections which may be queued awaiting acceptance by the
  542. server process; this number may be limited by the system.  Should a
  543. connection be requested while the queueis full, the connection will not
  544. be refused, but rather the individual messages which comprise the request
  545. will be ignored.  This gives a harried server time to make room in its
  546. pending connection queue while the client retries the connection request.
  547. Had the connection been returned with the ECONNREFUSED error, the client
  548. would be unable to tell if the server was up or not.  As it is now it is
  549. still possible to get the ETIMEDOUT error back, though this is unlikely.
  550. The backlog figure supplied with the listen call is currently limited by the
  551. system to a maximum of 5 pending connections on any one queue.  This avoids
  552. the problem of processes hogging systemresources by setting an infinite backlog,
  553. then ignoring all connection requests.
  554.    With a socket marked as listening, a server may accept a connection:
  555.  
  556.  
  557.     struct sockaddr_in from;
  558.      ...
  559.     fromlen = sizeof (from);
  560.     newsock = accept(s, (struct sockaddr *)&from, &fromlen);
  561.  
  562.  
  563. A new descriptor is returned on receiptof a connection (along with a new
  564. socket).  If the server wishes to find out who its client is, it may
  565. supply a buffer for the client socket'sname.   The value--result
  566. parameter fromlen is initialized by theserver to indicate how much space
  567. is associated with from, then modified on return to reflect the true size
  568. of the name.  If the client's name is not of interest,the second
  569. parameter may be a NULL pointer.
  570.    accept()normally blocks.  That is, accept() will not return until a
  571.  
  572. connection is available or the system call is interrupted by a signal6 to
  573. the process.  Further, there is no way for a process toindicate it will
  574. accept connections from only a specificindividual, or individuals.  It
  575. is up to the user process to consider who the connection is from and
  576. close down the connection if it does notwish to speak to the process.
  577. If the server process wants to accept connections on more than one
  578. socket, or wants to avoid blocking on the accept call, there are
  579. alternatives; they will be considered insection 3.4.
  580. ________________________________
  581.    6By default, the CTRL-C signalinterrupts the system calls, but the
  582.  
  583. application may change this, however.
  584.  
  585.  
  586. 36    Section 3.1                  AmiTCP/IP                   System Manual
  587.  
  588.  
  589.  
  590. 3.1.7   Data  Transfer
  591.  
  592.  
  593. With a connection established, data maybegin to flow.  To send and
  594. receive data there are a number of possible calls.   With the peer entity
  595. at each end of a connection anchored, auser can send or receive a
  596. message without specifying the peer.  The calls send()and recv() may be
  597. used:
  598.  
  599.  
  600.     send(s, buf, sizeof (buf), flags);
  601.     recv(s, buf, sizeof (buf), flags);
  602.  
  603.  
  604. While send() and recv() are virtually identical to the standard I/O
  605. routines, the extra flags argument is important.   Theflags, defined in
  606. sys/socket.h, may be specified as a non--zero value if one or more of the
  607. following is required:
  608.  
  609.  
  610.  MSG_OOB Send/receive out of band data.
  611.  
  612.  
  613.  MSG_PEEK Lookat data without reading.
  614.  
  615.  
  616.  MSG_DONTROUTESend data without routing packets.
  617.  
  618.  
  619.    Out of band data is a notion specific to stream sockets, and one which
  620. we will not immediately consider.  The option to have data sent without
  621. routing applied to the outgoing packetsis currently used only by the
  622. routing table management process, and isunlikely to be of interest to
  623. the casual user.  The ability to preview data is, however, of interest.
  624. When MSG_PEEK is specified with a recv() call, any data present is
  625. returned to the user, but treated as still ``unread''.  That is, the next
  626. recv() call applied to the socket will return the data previously
  627. previewed.
  628.  
  629.  
  630.  
  631. 3.1.8   Discarding Sockets
  632.  
  633.  
  634. Once a socket is no longer of interest,it may be discarded by applying a
  635. CloseSocket() to the descriptor,
  636.  
  637.  
  638.     CloseSocket(s);
  639.  
  640.  
  641. If data is associated with a socket which promises reliable delivery
  642. (e.g.  a stream socket) when a close takes place, the system will
  643. continue to attempt to transfer the data.   However, after a fairly long
  644. period of time, if the data is still undelivered, it will be discarded.
  645. Should a user have no use for any pending data, it may perform a
  646. shutdown() on the socket prior to closing it.   This call is of the form:
  647.  
  648.  
  649.     shutdown(s, how);
  650.  
  651.  
  652. where how is 0 if the user is no longerinterested in reading data, 1 if
  653. no more data will be sent, or 2 if no data is to be sent or received.
  654.  
  655.  
  656. System Manual                   AmiTCP/IP                   Section 3.1    37
  657.  
  658.  
  659.  
  660. 3.1.9   Connectionless Sockets
  661.  
  662. To this point we have been concerned mostly with sockets which follow a
  663. connection oriented model.  However, there is also support for
  664. connectionless interactions typical of the datagram facilities found in
  665. contemporary packet switched networks.  A datagram socket provides a
  666. symmetric interface to data exchange.  While processesare still likely
  667. to be client and server, there is no requirement for connection
  668. establishment.  Instead, each message includes the destination address.
  669.    Datagramsockets are created as before.  If a particular local address
  670. is needed, the bind operation must precede the first data transmission.
  671. Otherwise, the system will set the localaddress and/or port when data is
  672. first sent.  To send data, the sendto() call is used,
  673.  
  674.     sendto(s, buf, buflen, flags, (struct sockaddr *)&to, tolen);
  675.  
  676. The s, buf, buflen, and flags parametersare used as before.  The to and
  677. tolen values are used to indicate the address of the intended recipient
  678. of the message.  When using an unreliable datagram interface, it is
  679. unlikely that any errors will be reported to the sender.  When
  680. information is present locally to recognize a message that can not be
  681. delivered (for instance when a network is unreachable), the call will
  682. return -1 and the global value errno will contain an error number (See
  683. section 3.1.2 for discussion about errno).
  684.    To receive messages on an unconnected datagram socket, the recvfrom()
  685. call is provided:
  686.  
  687.     recvfrom(s, buf, buflen, flags, (struct sockaddr *)&from, &fromlen);
  688.  
  689. Once again, the fromlen parameter is handled in a value--result fashion,
  690. initially containing the size of the from buffer, and modified on return
  691. to indicate the actual size of the address from which the datagram was
  692. received.
  693.    In addition to the two calls mentioned above, datagram sockets may also
  694. use the connect() call to associate a socket with a specific destination
  695. address.  In this case, any data sent on the socket will automatically be
  696. addressed to the connected peer, and only data received from that peer
  697. will be delivered to the user.  Only one connected address is permitted
  698. for each socket at one time; a second connect() will change the
  699. destination address, and a connect() toa null address (family AF_UNSPEC)
  700. will disconnect.  Connect requests on datagram socketsreturn
  701. immediately, as this simply results in the system recording the peer's
  702. address (as compared to a stream socket,where a connect request
  703. initiates establishment of an end to endconnection).  accept() and listen()
  704. are not used with datagram sockets.
  705.    While a datagram socket is connected, errors from recent send() calls
  706. may be returned asynchronously.  These errors may be reported on
  707. subsequent operations on the socket, ora special socket option used with
  708. getsockopt(), SO_ERROR,may be used to interrogate the error status.  A
  709. select() for reading or writing will return true when an error indication
  710. has been received.  The next operation will return theerror, and the
  711. error status is cleared.  Other of the less important details of datagram
  712. sockets are described in section 3.4.
  713.  
  714.  
  715. 38    Section 3.1                  AmiTCP/IP                   System Manual
  716.  
  717.  
  718.  
  719. 3.1.10   Input/Output  Multiplexing
  720.  
  721. One last facility often used in developing applications is the ability to
  722. multiplex i/o requests among multiple sockets.   This is done using the
  723. select() call.  The select() call provided by AmiTCP/IPis actually a
  724. compile time inline function (or normalstub with compilers without
  725. inline facility) which calls the WaitSelect().   The WaitSelect() call is
  726. similar to the normal select() call, buthas one extra argument
  727. specifying a pointer to a signal mask for the signals which should break
  728. the selection (in addition to the timeouts and the break signal).  This
  729. makes possible to use WaitSelect() instead of normal Wait() as a driver
  730. for the applications event loop.  If the pointer is given as NULL the
  731. functionality is as with BSD select().  The inline (orstub) function for select*
  732.  *()
  733. actually just calls the WaitSelect() with last argument as NULL.
  734.    Here is abrief example of the usage of the WaitSelect():
  735.  
  736.  
  737.     #include <sys/time.h>
  738.     #include <sys/types.h>
  739.      ...
  740.  
  741.  
  742.     fd_set readmask, writemask, exceptmask;
  743.     struct timeval timeout;
  744.     ULONG signalmask;
  745.      ...
  746.     WaitSelect(nfds, &readmask, &writemask, &exceptmask, &timeout,
  747.                 &signalmask);
  748.  
  749.  
  750. WaitSelect() takes as arguments pointersto three sets, one for the set
  751. of file descriptors for which the callerwishes to be able to read data
  752. on, one for those descriptors to which data is to be written, and one for
  753. which exceptional conditions are pending; out-of-band data is the only
  754. exceptional condition currently implemented.   If the user is not
  755. interested in certain conditions (i.e.,read, write, or exceptions), the
  756. corresponding argument to the select() should be a NULL pointer.
  757.    Each setis actually a structure containing an array of long integer
  758. bit masks; the size of the array is setby the definition FD_SETSIZE. The
  759. array is long enough to hold one bit foreach of FD_SETSIZE file
  760. descriptors.
  761.    The macros FD_SET(fd, &mask) and FD_CLR(fd, &mask) have been provided
  762. for adding and removing file descriptorfd in the set mask.  The set
  763. should be zeroed before use, and the macro FD_ZERO(&mask) has been
  764. provided to clear the set mask.  The parameter nfds inthe select() call
  765. specifies the range of file descriptors(i.e.   one plusthe value of the
  766. largest descriptor) to be examined in aset.
  767.    A timeoutvalue may be specified if the selection is not to last more
  768. than a predetermined period of time.  If the fields intimeout are set to
  769. 0, the selection takes the form of a poll, returning immediately.  If the
  770.  
  771. last parameter is a NULL pointer, the selection will block indefinitely7.
  772. ________________________________
  773.    7To be more specific, a returntakes place only when a descriptor is
  774.  
  775. selectable, or when a signal is receivedby the caller, interrupting the
  776. system call.
  777.  
  778.  
  779. System Manual                   AmiTCP/IP                   Section 3.1    39
  780.  
  781.  
  782.  
  783.    The lastargument is a pointer to the mask specifying signals for which
  784. the WaitSelect() should break.  WaitSelect() normally returns the number
  785. of file descriptors selected; if the WaitSelect() call returns due to the
  786. timeout expiring, then the value 0 is returned.   If the WaitSelect()
  787. terminates because of an error or interruption, a -1 is returned with the
  788. error number in errno, and with the filedescriptor masks unchanged.  The
  789. signal mask is altered on return to holdthe bits for the signals which
  790. caused the break.
  791.    Assuminga successful return, the three sets will indicate which file
  792. descriptors are ready to be read from, written to, or have exceptional
  793. conditions pending.  The status of a file descriptor ina select mask may
  794. be tested with the FD_ISSET(fd, &mask) macro, which returns a non-zero
  795. value if fd is a member of the set mask,and 0 if it is not.
  796.    To determine if there are connections waiting on a socket to be used
  797. with an accept() call, select() can be used, followed by a
  798. FD_ISSET(fd, &mask) macro to check for read readiness on the appropriate
  799. socket.  If FD_ISSET() returns a non-zero value, indicating permission to
  800. read, then a connection is pending on the socket.
  801.    As an example, to read data from two sockets, s1 and s2 as it is
  802. available from each and with a one--second timeout, the following code
  803. might be used:
  804.  
  805.  
  806.     #include <sys/time.h>
  807.     #include <sys/types.h>
  808.     #include <sys/socket.h>
  809.      ...
  810.     fd_set read_template;
  811.     struct timeval wait;
  812.     int nb;
  813.     int s1,s2;
  814.     int maxfd;
  815.      ...
  816.     maxfd = s1 > s2 ? s1 : s2;
  817.     for (;;) -
  818.          wait.tv_sec = 1;         /* onesecond */
  819.          wait.tv_usec = 0;
  820.  
  821.  
  822.          FD_ZERO(&read_template);
  823.  
  824.  
  825.          FD_SET(s1,  &read_template);
  826.          FD_SET(s2,  &read_template);
  827.  
  828.  
  829.          nb = select(maxfd, &read_template, NULL, NULL, &wait);
  830.          if (nb <=0) -
  831.              /* An error occurred during the select, or
  832.                 the select timed out. */
  833.          "
  834.  
  835.  
  836.          if (FD_ISSET(s1, &read_template)) -
  837.              /* Socket #1 is ready to be readfrom. */
  838.          "
  839.  
  840.  
  841.          if (FD_ISSET(s2, &read_template)) -
  842.              /* Socket #2 is ready to be readfrom. */
  843.  
  844.  
  845. 40    Section 3.2                  AmiTCP/IP                   System Manual
  846.  
  847.  
  848.  
  849.          "
  850.     "
  851.  
  852.  
  853.    Note theusage of the select(), which calls WaitSelect() with NULL
  854. signal mask pointer.
  855.    In 4.2BSD, the arguments to select() were pointers to integers instead
  856. of pointers to fd_sets.  This type ofcall will still work as long as the
  857. number of file descriptors being examined is less than the number of bits
  858. in an integer; however, the methods illustrated above should be used in
  859. all current programs.
  860.    select()provides a synchronous multiplexing scheme.  Asynchronous
  861. notification of output completion, inputavailability, and exceptional
  862. conditions is possible through use of the SigIO and SigURG signals
  863. described in section 3.4.
  864.  
  865.  
  866.  
  867. 3.2     Network  Library  Routines
  868.  
  869.  
  870. The discussion in section 3.1 indicatedthe possible need to locate and
  871. construct network addresses when using the interprocess communication
  872. facilities in a distributed environment.  To aid in this task a number of
  873. routines have been added to the Amiga shared socket library.  In this
  874. section we will consider the routines provided to manipulate network
  875. addresses.
  876.    Locatinga service on a remote host requires many levels of mapping
  877. before client and server may communicate.   A service is assigned a name
  878. which is intended for human consumption;e.g.   ``the login server on host
  879. monet''.  This name, and the name of the peer host, must then be
  880. translated into network addresses whichare not necessarily suitable for
  881. human consumption.  Finally, the address must then usedin locating a
  882. physical location and route to the service.   The specifics of these three
  883. mappings are likely to vary between network architectures.  For instance,
  884. it is desirable for a network to not require hosts to be named in such a
  885. way that their physical location is known by the client host.  Instead,
  886. underlying services in the network may discover the actual location of the
  887. host at the time a client host wishes tocommunicate.  This ability to have
  888. hosts named in a location independent manner may induce overhead in connection
  889. establishment, as a discovery process must take place, but allows a host to
  890. be physically mobile without requiring it to notify its clientele of its current
  891. location.
  892.    Standardroutines are provided for:  mapping host names to network
  893. addresses, network names to network numbers, protocol names to protocol
  894. numbers, and service names to port numbers and the appropriate protocol
  895. to use in communicating with the serverprocess.   The file netdb.h must
  896. be included when using any of these routines.
  897.  
  898.  
  899.  
  900. 3.2.1   Host  Names
  901.  
  902.  
  903. An Internet host name to address mappingis represented by the hostent
  904. structure:
  905.  
  906.  
  907. System Manual                   AmiTCP/IP                   Section 3.2    41
  908.  
  909.  
  910.  
  911.     struct  hostent -
  912.          char    *h_name;         /* official name of host */
  913.          char    **h_aliases;     /* alias list */
  914.          int     h_addrtype;      /* host address type (e.g., AF_INET) */
  915.          int     h_length;       /* length of address */
  916.          char    **h_addr_list;  /* list of addresses, nullterminated */
  917.     ";
  918.  
  919.  
  920.     #define h_addr  h_addr_list[0]  /* first address, network byte order */
  921.  
  922.  
  923.    The routine gethostbyname() takes an Internet host name and returns a
  924. hostent structure, while the routine gethostbyaddr() maps Internet host
  925. addresses into a hostent structure.
  926.    The official name of the host and its public aliases are returned by
  927. these routines, along with the address type (family) and a null
  928. terminated list of variable length addresses.   This list of addresses is
  929. required because it is possible for a host to have many addresses, all
  930. having the same name.  The h_addr definition is provided for backward
  931. compatibility, and is defined to be thefirst address in the list of
  932. addresses in the hostent structure.
  933.    The database for these calls is provided either by the configuration
  934. file or by use of a name server.  Because of the differences in these
  935. databases and their access protocols, the information returned may
  936. differ.  When using the host table version of gethostbyname(), only one
  937. address will be returned, but all listedaliases will be included.  The
  938. name server version may return alternateaddresses, but will not provide
  939. any aliases other than one given as argument.
  940.  
  941.  
  942.  
  943. 3.2.2   Network  Names
  944.  
  945. As for host names, routines for mappingnetwork names to numbers, and
  946. back, are provided.  These routines return a netent structure:
  947.  
  948.  
  949.     /*
  950.      * Assumption here is that a network number
  951.      * fits in 32 bits -- probably a poor one.
  952.      */
  953.     struct  netent -
  954.          char    *n_name;         /* official name of net */
  955.          char    **n_aliases;     /* alias list */
  956.          int     n_addrtype;      /* net address type */
  957.          int     n_net;           /* networknumber, host byte order */
  958.     ";
  959.  
  960.  
  961.    The routines getnetbyname(), andgetnetbynumber() are the network
  962. counterparts to the host routines described above.   The routines uses
  963. data read from AmiTCP/IP configuration file.
  964.  
  965.  
  966.  
  967. 3.2.3   Protocol Names
  968.  
  969. For protocols, the protoent structure defines the protocol--name mapping
  970. used with the routines getprotobyname()and getprotobynumber():
  971.  
  972.  
  973. 42    Section 3.2                  AmiTCP/IP                   System Manual
  974.  
  975.  
  976.  
  977.     struct  protoent -
  978.          char    *p_name;         /* official protocol name */
  979.          char    **p_aliases;     /* alias list */
  980.          int     p_proto;         /* protocol number */
  981.     ";
  982.  
  983.  
  984.  
  985. 3.2.4   Service  Names
  986.  
  987.  
  988. Information regarding services is a bitmore complicated.  A service is
  989. expected to reside at a specific ``port'' and employ a particular
  990. communication protocol.  This view is consistent with the Internet
  991. domain, but inconsistent with other network architectures.  Further, a
  992. service may reside on multiple ports.  If this occurs,the higher level
  993. library routines will have to be bypassed or extended.  A service mapping
  994. is described by the servent structure:
  995.  
  996.  
  997.  
  998.     struct  servent -
  999.          char    *s_name;         /* official service name */
  1000.          char    **s_aliases;     /* alias list */
  1001.          int     s_port;          /*port number, network byte order */
  1002.          char    *s_proto;       /* protocol to use */
  1003.     ";
  1004.  
  1005.  
  1006.    The routine getservbyname() maps service names to a servent structure
  1007. by specifying a service name and, optionally, a qualifying protocol.
  1008. Thus the call
  1009.  
  1010.  
  1011.     sp = getservbyname("telnet", NULL);
  1012.  
  1013.  
  1014. returns the service specification for atelnet server using any protocol,
  1015. while the call
  1016.  
  1017.  
  1018.     sp = getservbyname("telnet", "tcp");
  1019.  
  1020.  
  1021. returns only that telnet server which uses the TCP protocol.  The routine
  1022. getservbyport() is also provided.  The getservbyport()routine has an
  1023. interface similar to that provided by getservbyname(); an optional
  1024. protocol name may be specified to qualify lookups.
  1025.  
  1026.  
  1027.  
  1028. 3.2.5   Miscellaneous
  1029.  
  1030.  
  1031. With the support routines described above, an Internet application
  1032. program should rarely have to deal directly with addresses.  This allows
  1033. services to be developed as much as possible in a network independent
  1034. fashion.  It is clear, however, that purging all network dependencies is
  1035. very difficult.  So long as the user is required to supply network
  1036. addresses when naming services and sockets there will always some network
  1037. dependency in a program.  For example, the normal codeincluded in client
  1038. programs, such as the remote login program, is as follows:
  1039.  
  1040.  
  1041. System Manual                   AmiTCP/IP                   Section 3.2    43
  1042.  
  1043.  
  1044.  
  1045. Remote Login Client Code
  1046.  
  1047.     #include <sys/types.h>
  1048.     #include <sys/socket.h>
  1049.     #include <netinet/in.h>
  1050.     #include <stdio.h>
  1051.     #include <netdb.h>
  1052.      ...
  1053.     int main(int argc, char *argv[])
  1054.     -
  1055.             struct sockaddr_in server;
  1056.             struct servent *sp;
  1057.             struct hostent *hp;
  1058.             int s;
  1059.              ...
  1060.             sp = getservbyname("login","tcp");
  1061.             if (sp == NULL) -
  1062.                fprintf(stderr, "rlogin: tcp/login: unknown service"n");
  1063.                exit(1);
  1064.             "
  1065.             hp = gethostbyname(argv[1]);
  1066.             if (hp == NULL) -
  1067.                fprintf(stderr, "rlogin: %s: unknown host"n",argv[1]);
  1068.                exit(2);
  1069.             "
  1070.             bzero((char *)&server, sizeof (server));
  1071.             server.sin_port = sp->s_port;
  1072.             bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  1073.             server.sin_family = hp->h_addrtype;
  1074.  
  1075.  
  1076.             s = socket(AF_INET, SOCK_STREAM, 0);
  1077.             if (s < 0) -
  1078.                perror("rlogin: socket");
  1079.                exit(3);
  1080.             "
  1081.              ...
  1082.             /* Connect does the bind()for us */
  1083.  
  1084.  
  1085.             if (connect(s, (struct sockaddr *)&server, sizeof (server)) < 0) -
  1086.                perror("rlogin: connect");
  1087.                exit(5);
  1088.             "
  1089.              ...
  1090.     "
  1091.  
  1092.  
  1093.  
  1094.    (This example will be considered in more detail in section 3.3.)
  1095.  
  1096.  
  1097.  
  1098.   If we wanted to make the remote login program independent of the
  1099. Internet protocols and addressing schemewe would be forced to add a
  1100. layer of routines which masked the network dependent aspects from the
  1101. mainstream login code.  For the current facilities available in the
  1102. system this does not appear to be worthwhile.
  1103.    Aside from the address-related data base routines, there are several
  1104. other routines available in the run-timelibrary which are of interest to
  1105.  
  1106.  
  1107. 44    Section 3.3                  AmiTCP/IP                   System Manual
  1108.  
  1109.  
  1110.  
  1111. users.  These are intended mostly to simplify manipulation of names and
  1112. addresses.  The routines for manipulating variable length byte strings
  1113. and handling byte swapping of network addresses and values are summarized
  1114.  
  1115. below:8.
  1116.  
  1117.  
  1118. bcmp(s1, s2, n)
  1119.  
  1120.     Compare byte-strings; 0 if same, not 0 otherwise.
  1121.  
  1122. bcopy(s1, s2, n)
  1123.  
  1124.     Copy n bytes from s1 to s2.
  1125.  
  1126. bzero(base, n)
  1127.  
  1128.     Zero-fill n bytes starting at base.
  1129.  
  1130. htonl(val)
  1131.  
  1132.     Convert 32-bit quantity from host to network byte order.
  1133.  
  1134. htons(val)
  1135.  
  1136.     Convert 16-bit quantity from host to network byte order.
  1137.  
  1138. ntohl(val)
  1139.  
  1140.     Convert 32-bit quantity from network to host byte order.
  1141.  
  1142. ntohs(val)
  1143.  
  1144.     Convert 16-bit quantity from network to host byte order.
  1145.  
  1146.  
  1147.    The byteswapping routines are provided because the operating system
  1148. expects addresses to be supplied in network order.   Onsome
  1149. architectures, such as the VAX, host byte ordering is different than
  1150. network byte ordering.  Consequently, programs are sometimes required to
  1151. byte swap quantities.  The library routines which return network
  1152. addresses provide them in network orderso that they may simply be copied
  1153. into the structures provided to the system.   This implies users should
  1154. encounter the byte swapping problem onlywhen interpreting network
  1155. addresses.  For example, if an Internet port is to be printed out the
  1156. following code would be required:
  1157.  
  1158.     printf("port number %d"n", ntohs(sp->s_port));
  1159.  
  1160. On machines where unneeded (as on Amiga)these routines are defined as
  1161. null macros.
  1162.  
  1163.  
  1164.  
  1165. 3.3     Client/Server Model
  1166.  
  1167.  
  1168. The most commonly used paradigm in constructing distributed applications
  1169. is the client/server model.  In this scheme client applications request
  1170. services from a server process.  This implies an asymmetry in
  1171. establishing communication between the client and server which has been
  1172. ________________________________
  1173.    8The byte string functions areprovided by the C-compiler.  The byte
  1174.  
  1175. order functions are provided as preprocessor macros.
  1176.  
  1177.  
  1178. System Manual                   AmiTCP/IP                   Section 3.3    45
  1179.  
  1180.  
  1181.  
  1182. examined in section 3.1.  In this section we will lookmore closely at
  1183. the interactions between client and server, and consider some of the
  1184. problems in developing client and serverapplications.
  1185.    The client and server require a well known set of conventions before
  1186. service may be rendered (and accepted).  This set of conventions
  1187. comprises a protocol which must be implemented at both ends of a
  1188. connection.  Depending on the situation, the protocol may be symmetric or
  1189. asymmetric.  In a symmetric protocol, either side may play the master or
  1190. slave roles.  In an asymmetric protocol, one side is immutably recognized
  1191. as the master, with the other as the slave.   An example of a symmetric
  1192. protocol is the TELNET protocol used inthe Internet for remote terminal
  1193. emulation.  An example of an asymmetric protocol is theInternet file
  1194. transfer protocol, FTP. No matter whether the specific protocol used in
  1195. obtaining a service is symmetric or asymmetric, when accessing a service there
  1196. is a ``client process'' and a ``server process''.   Wewill first consider the
  1197. properties of server processes, then client processes.
  1198.    A serverprocess normally listens at a well known address for service
  1199. requests.  That is, the server process remains dormantuntil a connection
  1200. is requested by a client's connection tothe server's address.  At such a
  1201. time the server process ``wakes up'' andservices the client, performing
  1202. whatever appropriate actions the clientrequests of it.
  1203.    Alternative schemes which use a service server may be used to eliminate
  1204. a flock of server processes clogging thesystem while remaining dormant
  1205. most of the time.  For Internet servers in 4.3BSD, thisscheme has been
  1206. implemented via inetd, the so called ``internet super-server.''  Inetd
  1207. listens at a variety of ports, determined at start-up by reading a
  1208. configuration file.  When a connection is requested toa port on which
  1209. inetd is listening, inetd executes the appropriate server program to
  1210. handle the client.  Inetd will be described in more detail in section
  1211. 3.4.
  1212.  
  1213.  
  1214.  
  1215. 3.3.1   Servers
  1216.  
  1217. In 4.3BSD most servers are accessed at well known Internet addresses or
  1218. UNIX domain names.  For example, the remote login server's main loop is
  1219. of the form shown below (AmiTCP/IP way):
  1220.  
  1221.  
  1222.     main(int argc, char *argv)
  1223.     -
  1224.          int f;
  1225.          struct sockaddr_in from;
  1226.          struct servent *sp;
  1227.  
  1228.  
  1229.          sp = getservbyname("login", "tcp");
  1230.          if (sp ==NULL) -
  1231.              fprintf(stderr, "rlogind: tcp/login: unknown service"n");
  1232.              exit(1);
  1233.          "
  1234.           ...
  1235.  
  1236.  
  1237.          sin.sin_port = sp->s_port;  /* Restricted port */
  1238.           ...
  1239.  
  1240.  
  1241. 46    Section 3.3                  AmiTCP/IP                   System Manual
  1242.  
  1243.  
  1244.  
  1245.          f = socket(AF_INET, SOCK_STREAM, 0);
  1246.           ...
  1247.          if (bind(f, (struct sockaddr *) &sin, sizeof (sin)) < 0) -
  1248.           ...
  1249.          "
  1250.           ...
  1251.          listen(f, 5);
  1252.          for (;;)-
  1253.              int g, len = sizeof (from);
  1254.  
  1255.  
  1256.              g = accept(f, (struct sockaddr *)&from, &len);
  1257.              if (g < 0) -
  1258.                  if (errno != EINTR)
  1259.                       syslog(LOG_ERR, "rlogind: accept: %s", errors[errno]);
  1260.                  continue;
  1261.              "
  1262.              /*
  1263.               * AmiTCP code follows...
  1264.               */
  1265.              id = ReleaseSocket(g, UNIQUE_ID);
  1266.              startit(id, &from);
  1267.          "
  1268.     "
  1269.  
  1270.    The firststep taken by the server is look up its service definition:
  1271.  
  1272.     sp = getservbyname("login", "tcp");
  1273.     if (sp == NULL) -
  1274.          fprintf(stderr, "rlogind: tcp/login: unknown service"n");
  1275.          exit(1);
  1276.     "
  1277.  
  1278. The result of the getservbyname call isused in later portions of the
  1279. code to define the Internet port at which it listens for service requests
  1280. (indicated by a connection).
  1281.    Once a server has established a pristine environment, it creates a
  1282. socket and begins accepting service requests.   The bind() call is
  1283. required to insure the server listens atits expected location.
  1284.    The mainbody of the loop is fairly simple:
  1285.  
  1286.     for (;;) -
  1287.          int g, len = sizeof (from);
  1288.  
  1289.  
  1290.          g = accept(f, (struct sockaddr *)&from, &len);
  1291.          if (g < 0) -
  1292.              if (errno != EINTR)
  1293.                  syslog(LOG_ERR, "rlogind: accept: %s", errors[errno]);
  1294.              continue;
  1295.          "
  1296.          /*
  1297.           * AmiTCP code follows...
  1298.           */
  1299.          id = ReleaseSocket(g, UNIQUE_ID);
  1300.          startit(id,  &from);
  1301.     "
  1302.  
  1303.  
  1304. System Manual                   AmiTCP/IP                   Section 3.3    47
  1305.  
  1306.  
  1307.  
  1308.    An accept() call blocks the server until a client requests service.
  1309. This call could return a failure statusif the call is interrupted by a
  1310. signal such as CTRL-C (to be discussed in section 3.4).  Therefore, the
  1311. return value from accept() is checked toinsure a connection has actually
  1312. been established.
  1313.    With a connection in hand, servers using AmiTCP/IP socket library,
  1314. this new socket is released to an external list inside AmiTCP/IP process
  1315. via ReleaseSocket() call.  ReleaseSocket() returns an id (unique if
  1316. requested).  startit() starts a new AmigaOS task and informs the id for
  1317. it.  This new task then uses ObtainSocket() with id asargument to
  1318. receive the socket.  The address of the client is alsohandled the new
  1319. task because it requires it in authenticating clients.
  1320.  
  1321.  
  1322.  
  1323. 3.3.2   Clients
  1324.  
  1325. The client side of the remote login service was shown earlier in section
  1326. 3.2.  One can see the separate, asymmetric roles of theclient and server
  1327. clearly in the code.  The server is a passive entity, listening for
  1328. client connections, while the client process is an active entity,
  1329. initiating a connection when invoked.
  1330.    Let us consider more closely the steps taken by the client remote login
  1331. process.  As in the server process, the first step is to locate the
  1332. service definition for a remote login:
  1333.  
  1334.  
  1335.     sp = getservbyname("login", "tcp");
  1336.     if (sp == NULL) -
  1337.          fprintf(stderr, "rlogin: tcp/login: unknown service"n");
  1338.          exit(1);
  1339.     "
  1340.  
  1341.    Next thedestination host is looked up with a gethostbyname() call:
  1342.  
  1343.  
  1344.     hp = gethostbyname(argv[1]);
  1345.     if (hp == NULL) -
  1346.          fprintf(stderr, "rlogin: %s: unknown host"n", argv[1]);
  1347.          exit(2);
  1348.     "
  1349.  
  1350. With this accomplished, all that is required is to establish a connection
  1351. to the server at the requested host andstart up the remote login
  1352. protocol.  The address buffer is filled in with the Internet address and
  1353. rlogin port number of the foreign host.
  1354.  
  1355.  
  1356.     bzero((char *)&server, sizeof (server));
  1357.     server.sin_port = sp->s_port;
  1358.     bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
  1359.     server.sin_family = hp->h_addrtype;
  1360.  
  1361. A socket is created, and a connection initiated.   Notethat connect()
  1362. implicitly performs a bind() call, because s is unbound.
  1363.  
  1364.  
  1365.     s = socket(hp->h_addrtype, SOCK_STREAM, 0);
  1366.     if (s < 0) -
  1367.  
  1368.  
  1369. 48    Section 3.3                  AmiTCP/IP                   System Manual
  1370.  
  1371.  
  1372.  
  1373.          perror("rlogin: socket");
  1374.          exit(3);
  1375.     "
  1376.      ...
  1377.     if (connect(s, (struct sockaddr *) &server,
  1378.              sizeof (server)) < 0) -
  1379.          perror("rlogin: connect");
  1380.          exit(4);
  1381.     "
  1382.  
  1383. The details of the remote login protocolwill not be considered here.
  1384.  
  1385.  
  1386.  
  1387. 3.3.3   Connectionless Servers
  1388.  
  1389. While connection-based services are thenorm, some services are based on
  1390. the use of datagram sockets.  One, in particular, is the ``rwho'' service
  1391. which provides users with status information for hosts connected to a
  1392. local area network.  This service, while predicated onthe ability to
  1393. broadcast information to all hosts connected to a particular network, is
  1394. of interest as an example usage of datagram sockets.
  1395.    A user onany machine running the rwho server may find out the current
  1396. status of a machine with the ruptime program.   The output generated is
  1397. illustrated below.
  1398.  
  1399.  
  1400.  
  1401. Ruptime Output
  1402.  
  1403. arpa         up   9:45,        5 users, load    1.15,    1.39,    1.31
  1404. cad          up    2+12:04,     8 users, load    4.67,    5.13,    4.59
  1405. calder       up    10:10,       0 users,load    0.27,    0.15,    0.14
  1406. dali         up   2+06:28,     9 users, load   1.04,    1.20,    1.65
  1407. degas       up    25+09:48,    0 users, load    1.49,    1.43,    1.41
  1408. ear          up    5+00:05,     0 users, load    1.51,    1.54,    1.56
  1409. ernie     down   0:24
  1410. esvax     down   17:04
  1411. ingres    down   0:26
  1412. kim          up    3+09:16,     8 users, load    2.03,    2.46,    3.11
  1413. matisse     up    3+06:18,    0 users, load    0.03,    0.03,    0.05
  1414. medea       up    3+09:39,     2 users,load    0.35,    0.37,    0.50
  1415. merlin    down   19+15:37
  1416. miro         up   1+07:20,     7 users, load   4.59,    3.28,    2.12
  1417. monet       up    1+00:43,     2 users,load    0.22,    0.09,    0.07
  1418. oz         down   16:09
  1419. statvax     up    2+15:57,    3 users, load    1.52,    1.81,    1.86
  1420. ucbvax      up    9:34,        2 users, load    6.08,   5.16,    3.28
  1421.  
  1422.  
  1423.    Status information for each host is periodically broadcast by rwho
  1424. server processes on each machine.  The same server process also receives
  1425. the status information and uses it to update a database.  This database
  1426. is then interpreted to generate the status information for each host.
  1427. Servers operate autonomously, coupled only by the local network and its
  1428. broadcast capabilities.
  1429.    Note thatthe use of broadcast for such a task is fairly inefficient,
  1430. as all hosts must process each message,whether or not using an rwho
  1431.  
  1432.  
  1433. System Manual                   AmiTCP/IP                   Section 3.3    49
  1434.  
  1435.  
  1436.  
  1437. server.  Unless such a service is sufficiently universal and is
  1438. frequently used, the expense of periodicbroadcasts outweighs the
  1439. simplicity.
  1440.  
  1441.    The rwhoserver, in a simplified form, is pictured next9:
  1442.  
  1443.  
  1444.     BYTE alrmsig;
  1445.  
  1446.  
  1447.     main()
  1448.     -
  1449.          long on;
  1450.          fd_set readfds;
  1451.           ...
  1452.          sp = getservbyname("who", "udp");
  1453.          sin.sin_port = sp->s_port;
  1454.          net = getnetbyname("localnet");
  1455.          sin.sin_addr = inet_makeaddr(INADDR_ANY, net);
  1456.           ...
  1457.          s = socket(AF_INET, SOCK_DGRAM, 0);
  1458.           ...
  1459.          on = 1;
  1460.          if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) -
  1461.              syslog(LOG_ERR, "rwhod: setsockopt SO_BROADCAST: %s",
  1462.                     strerror(errno));
  1463.              exit(1);
  1464.          "
  1465.          bind(s, (struct sockaddr *) &sin, sizeof (sin));
  1466.           ...
  1467.          alrmsig =AllocSignal(-1);
  1468.          onalrm();/* activate and handle periodic alarm system */
  1469.  
  1470.  
  1471.          FD_ZERO(&readfds);
  1472.          FD_SET(s, &readfds);
  1473.  
  1474.  
  1475.          for (;;)-
  1476.              struct whod wd;
  1477.              struct sockaddr_in from;
  1478.              int n, cc, whod, len = sizeof (from);
  1479.              ULONG alrmmask;
  1480.  
  1481.  
  1482.              alrmmask = 1 << alrmsig;
  1483.              n = WaitSelect(s, &readfds, NULL,NULL, NULL, &alrmmask);
  1484.              if (n < 0) -
  1485.                  syslog(LOG_ERR, "rwhod: WaitSelect: %s", strerror(errno));
  1486.                  exit(1);
  1487.              "
  1488.              if (alrmmask)
  1489.                onalrm(); /* handles the alarm */
  1490.              if (n > 0) -
  1491.                  cc = recvfrom(s, (char *)&wd, sizeof (wd), 0,
  1492.                                 (struct sockaddr *)&from, &len);
  1493.                  if (cc <= 0) -
  1494. ________________________________
  1495.    9A real code must always test the return values of various services
  1496.  
  1497. against errors.  Thes e tests are partly omitted from this code to show
  1498. the matters important to this section.
  1499.  
  1500.  
  1501. 50    Section 3.3                  AmiTCP/IP                   System Manual
  1502.  
  1503.  
  1504.  
  1505.                       if (cc < 0)
  1506.                           syslog(LOG_ERR, "rwhod: recv: %s", strerror(errno));
  1507.                       continue;
  1508.                  "
  1509.                  if (from.sin_port != sin.sin_port) -
  1510.                       syslog(LOG_ERR, "rwhod: %ld: bad from port",
  1511.                              ntohs(from.sin_port));
  1512.                       continue;
  1513.                  "
  1514.                  ...
  1515.                  if (!verify(wd.wd_hostname)) -
  1516.                       syslog(LOG_ERR, "rwhod: malformed host name from %lx",
  1517.                              ntohl(from.sin_addr.s_addr));
  1518.                       continue;
  1519.                  "
  1520.                  (void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
  1521.                  whod = open(path, O_WRONLY _ O_CREAT _ O_TRUNC, 0666);
  1522.                  ...
  1523.                  (void) time(&wd.wd_recvtime);
  1524.                  (void) write(whod, (char *)&wd, cc);
  1525.                  (void) close(whod);
  1526.              "
  1527.          "
  1528.     "
  1529.  
  1530.  
  1531.    There aretwo separate tasks performed by the server.  The first task
  1532. is to act as a receiver of status information broadcast by other hosts on
  1533. the network.  This job is carried out in the main loopof the program.
  1534. Packets received at the rwho port are interrogated to insure they've been
  1535. sent by another rwho server process, then are time stamped with their
  1536. arrival time and used to update a file indicating the status of the host.
  1537. When a host has not been heard from foran extended period of time, the
  1538. database interpretation routines assumethe host is down and indicate
  1539. such on the status reports.  This algorithm is prone toerror as a server
  1540. may be down while a host is actually up,but serves our current needs.
  1541.    The second task performed by the server is to supply information
  1542. regarding the status of its host.  This involves periodically acquiring
  1543. system status information, packaging itup in a message and broadcasting
  1544. it on the local network for other rwho servers to hear.  The supply
  1545. function is triggered by a timer and runs off a signal.  Locating the
  1546. system status information is somewhat involved, but uninteresting.
  1547. Deciding where to transmit the resultantpacket is somewhat
  1548. problematical, however.
  1549.    Status information must be broadcast on the local network.  For
  1550. networks which do not support the notionof broadcast another scheme must
  1551. be used to simulate or replace broadcasting.   One possibility is to
  1552. enumerate the known neighbors (based onthe status messages received from
  1553. other rwho servers).  This, unfortunately, requires some bootstrapping
  1554. information, for a server will have no idea what machines are its
  1555. neighbors until it receives status messages from them.  Therefore, if all
  1556. machines on a net are freshly booted, nomachine will have any known
  1557. neighbors and thus never receive, or send, any status information.  This
  1558. is the identical problem faced by the routing table management process in
  1559.  
  1560.  
  1561. System Manual                   AmiTCP/IP                   Section 3.4    51
  1562.  
  1563.  
  1564.  
  1565. propagating routing status information.  The standard solution, unsatisfactory
  1566. as it may be, is to inform one or more servers of known neighbors and request
  1567. that theyalways communicate with these neighbors.   Ifeach server has at least
  1568. one neighbor supplied to it, status information may then propagate through
  1569. a neighbor to hosts which are not (possibly) directly neighbors.  If the server
  1570. is able to support networks which provide a broadcast capability, as well as
  1571. those which do not, then networks with an arbitrary topology may share status
  1572.  
  1573. information10
  1574.    It is important that software operating in a distributed environment
  1575. not have any site-dependent informationcompiled into it.  This would
  1576. require a separate copy of the server ateach host and make maintenance a
  1577. severe headache.  4.3BSD attempts to isolate host-specific information
  1578. from applications by providing system calls which return the necessary
  1579.  
  1580. information11.  A mechanism exists, in the form of an IoctlSocket() call,
  1581. for finding the collection of networks to which a host is directly
  1582. connected.  Further, a local network broadcasting mechanism has been
  1583. implemented at the socket level.  Combining these two features allows a
  1584. process to broadcast on any directly connected local network which
  1585. supports the notion of broadcasting in asite independent manner.  This allows
  1586. 4.3BSD to solve the problem of decidinghow to propagate status information
  1587. in the case of rwho, or more generally in broadcasting:  Such status information
  1588. is broadcast to connected networks at the socket level, where the connected
  1589. networks have been obtained via the appropriate ioctl calls.  The specifics
  1590. of such broadcastings are complex, however, and will be covered in section
  1591. 3.4.
  1592.  
  1593.  
  1594.  
  1595. 3.4     Advanced Topics
  1596.  
  1597.  
  1598. A number of facilities have yet to be discussed.   Formost users of the
  1599. AmiTCP/IP the mechanisms already described will suffice in constructing
  1600. distributed applications.  However, others will find the need to utilize
  1601. some of the features which we consider in this section.
  1602.  
  1603.  
  1604.  
  1605. 3.4.1   OutOf Band Data
  1606.  
  1607. The stream socket abstraction includes the notion of ``out of band''
  1608. data.  Out of band data is a logically independent transmission channel
  1609. associated with each pair of connected stream sockets.  Out of band data
  1610. is delivered to the user independently of normal data.  The abstraction
  1611. defines that the out of band data facilities must support the reliable
  1612. delivery of at least one out of band message at a time.  This message may
  1613. contain at least one byte of data, and at least one message may be
  1614. pending delivery to the user at any onetime.   For communications
  1615. protocols which support only in-band signaling (i.e.  the urgent data is
  1616. ________________________________
  1617.   10One must, however, be concerned about loops.  That is, if a host is
  1618.  
  1619. connected to multiple networks, it willreceive status information from
  1620. itself.  This can lead to an endless, wasteful, exchange of information.
  1621.   11An example of such a system call is the gethostname() call which returns
  1622.  
  1623. the host's official name.
  1624.  
  1625.  
  1626. 52    Section 3.4                  AmiTCP/IP                   System Manual
  1627.  
  1628.  
  1629.  
  1630. delivered in sequence with the normal data), the system normally extracts
  1631. the data from the normal data stream andstores it separately.  This allows
  1632. users to choose between receiving the urgent data in order and receiving it
  1633. out of sequence without having to bufferall the intervening data.  It is possib*
  1634.  *le
  1635. to ``peek'' (via MSG_PEEK) at out of band data.  If the socket has an owner,
  1636. a signal is generated when the protocolis notified of its existence.  A process
  1637. can set the task to be informed by a signal via the appropriate IoctlSocket()
  1638. and SetSocketSignals() calls, as described below in section 3.4.3.  If multiple
  1639. sockets may have out of band data awaiting delivery, a select() call for except*
  1640.  *ional
  1641. conditions may be used to determine those sockets with such data pending.  Neit*
  1642.  *her
  1643. the signal nor the select() indicate theactual arrival of the out-of-band
  1644. data, but only notification that it is pending.
  1645.    In addition to the information passed, a logical mark is placed in the
  1646. data stream to indicate the point at which the out of band data was
  1647.  
  1648. sent12.  The remote login and remote shell applications use this facility
  1649. to propagate signals between client andserver processes.  When a signal
  1650. flushs any pending output from the remote process(es), all data up to the
  1651. mark in the data stream is discarded.
  1652.    To send an out of band message the MSG_OOB flag is supplied to a send()
  1653. or sendto() calls, while to receive outof band data MSG_OOB should be
  1654. indicated when performing a recvfrom() or recv() call.  To find out if
  1655. the read pointer is currently pointing at the mark in the data stream,
  1656. the SIOCATMARK ioctl is provided:
  1657.  
  1658.  
  1659.     IoctlSocket(s, SIOCATMARK, &yes);
  1660.  
  1661. If yes is a 1 on return, the next read will return data after the mark.
  1662. Otherwise (assuming out of band data hasarrived), the next read will
  1663. provide data sent by the client prior totransmission of the out of band
  1664. signal.  The routine used in the remote login process to flush output on
  1665. receipt of an interrupt or quit signal is shown below:
  1666.  
  1667.     #include <sys/ioctl.h>
  1668.     #include <sys/socket.h>
  1669.      ...
  1670.     oob()
  1671.     -
  1672.          int mark;
  1673.          char waste[BUFSIZ];
  1674.  
  1675.  
  1676.          /* flushterminal I/O on receipt of out of band data */
  1677.  
  1678.  
  1679.          for (;;)-
  1680.              if (IoctlSocket(rem, SIOCATMARK,&mark) < 0) -
  1681.                  perror("IoctlSocket");
  1682.                  break;
  1683.              "
  1684.              if (mark)
  1685.                  break;
  1686.              recv(rem, waste, sizeof (waste),0);
  1687. ________________________________
  1688.   12AmiTCP/IP follows the BSD interpretation of the RFC 793 in which the
  1689.  
  1690. concept of out-of-band data is introduced.   The BSD interpretation is in
  1691. conflict with (later) defined Host Requirements laid down in RFC 1122.
  1692.  
  1693.  
  1694. System Manual                   AmiTCP/IP                   Section 3.4    53
  1695.  
  1696.  
  1697.  
  1698.          "
  1699.          if (recv(rem, &mark, 1, MSG_OOB) < 0) -
  1700.              perror("recv");
  1701.               ...
  1702.          "
  1703.           ...
  1704.     "
  1705.  
  1706.    The normal data up to the mark if first read (discarding it), then the
  1707. out-of-band byte is read.
  1708.    A processmay also read or peek at the out-of-band data without first
  1709. reading up to the mark.  This is more difficult when the underlying
  1710. protocol delivers the urgent data in-band with the normal data, and only
  1711. sends notification of its presence aheadof time (e.g., the TCP protocol
  1712. used to implement streams in the Internet domain).   With such protocols,
  1713. the out-of-band byte may not yet have arrived when a recv() is done with
  1714. the MSG_OOB flag.  In that case, the call will return an error of
  1715. EWOULDBLOCK. Worse, there may be enoughin-band data in the input buffer
  1716. that normal flow control prevents the peer from sending the urgent data
  1717. until the buffer is cleared.  The process must then read enough of the
  1718. queued data that the urgent data may bedelivered.
  1719.    Certain programs that use multiple bytes of urgent data and must handle
  1720. multiple urgent signals (e.g., telnet) need to retain the position of
  1721. urgent data within the stream.  This treatment is available as a
  1722. socket-level option, SO_OOBINLINE; see function reference for
  1723. setsockopt() for usage.  With this option, the positionof urgent data
  1724. (the ``mark'') is retained, but the urgent data immediately follows the
  1725. mark within the normal data stream returned without the MSG_OOB flag.
  1726. Reception of multiple urgent indicationscauses the mark to move, but no
  1727. out-of-band data are lost.
  1728.  
  1729.  
  1730.  
  1731. 3.4.2   Non-Blocking Sockets
  1732.  
  1733. It is occasionally convenient to make use of sockets which do not block;
  1734. that is, I/O requests which cannot complete immediately and would
  1735. therefore cause the process to be suspended awaiting completion are not
  1736. executed, and an error code is returned.  Once a sockethas been created
  1737. via the socket() call, it may be markedas non-blocking by IoctlSocket()
  1738. as follows:
  1739.  
  1740.     #include <sys/ioctl.h>
  1741.      ...
  1742.     int      s;
  1743.      long     yes = TRUE;
  1744.      ...
  1745.     s = socket(AF_INET, SOCK_STREAM, 0);
  1746.      ...
  1747.     if (IoctlSocket(s, FIONBIO, &yes) < 0)
  1748.          perror("IoctlSocket FIONBIO");
  1749.          exit(1);
  1750.     "
  1751.      ...
  1752.  
  1753.    When performing non-blocking I/O on sockets, one must be careful to
  1754. check for the error EWOULDBLOCK (storedin the global variable errno),
  1755.  
  1756.  
  1757. 54    Section 3.4                  AmiTCP/IP                   System Manual
  1758.  
  1759.  
  1760.  
  1761. which occurs when an operation would normally block, but the socket it
  1762. was performed on is marked as non-blocking.   In particular, accept(),
  1763. connect(), send(), sendto(), recv() andrecvto() can all return
  1764. EWOULDBLOCK, and processes should be prepared to deal with such return
  1765. codes.  If an operation such as a send() cannot be donein its entirety,
  1766. but partial writes are sensible (for example, when using a stream
  1767. socket), the data that can be sent immediately will be processed, and the
  1768. return value will indicate the amount actually sent.
  1769.  
  1770.  
  1771.  
  1772. 3.4.3   Signal Driven Socket I/O
  1773.  
  1774. The AmiTCP/IP allows a task to be notified via a signal when a socket
  1775. has either normal or out-of-band data waiting to be read.  Use of this
  1776. facility requres four steps:
  1777.  
  1778.  1. The signals to be used must be allocated with Exec AllocSignal()
  1779.     call.
  1780.  
  1781.  2. The allocated signal(s) must be registered to the AmiTCP/IP with the
  1782.     SetSocketSignals() call.  The signals registered with
  1783.     SetSocketSignals() affect all sockets of the calling task, so this is
  1784.     usually done only after OpenLibrary() call.
  1785.  
  1786.  3. The owner of the socket must be set to the task itself (note that the
  1787.     owner of a socket is unspecified by default).  This is accomplished
  1788.     by the use of an IoctlSocket() call.
  1789.  
  1790.  4. Asynchronous notification for the socket must be enabled with another
  1791.     IoctlSocket() call
  1792.  
  1793. Note that it is application's responsibility to react on received
  1794. signals.
  1795.    Sample code to allow a given process to receive information on pending
  1796. I/O requests as they occur for a sockets is given below:
  1797.  
  1798.     #include <exec/tasks.h>
  1799.     #include <sys/ioctl.h>
  1800.      ...
  1801.     BYTE SIGIO = -1, SIGURG = -1;
  1802.      ...
  1803.     struct Task *thisTask = FindTask(NULL); /* our task pointer */
  1804.     long yes = TRUE;
  1805.  
  1806.  
  1807.     /* Allocate signals for asynchronous notification */
  1808.  
  1809.  
  1810.     if ((SIGIO = AllocSignal(-1)) == -1) -
  1811.          fprintf(stderr, "allocSignal failed."n");
  1812.          exit(1);
  1813.     "
  1814.     atexit(freeSignals); /* free allocated signals on exit */
  1815.     if ((SIGURG = AllocSignal(-1)) == -1) -
  1816.          fprintf(stderr, "allocSignal failed."n");
  1817.          exit(1);
  1818.     "
  1819.  
  1820.  
  1821. System Manual                   AmiTCP/IP                   Section 3.4    55
  1822.  
  1823.  
  1824.  
  1825.     /* Set socket signals for this task */
  1826.  
  1827.  
  1828.     SetSocketSignals(SIGBREAKF_CTRL_C, 1 << SIGIO, 1 << SIGURG);
  1829.  
  1830.  
  1831.     /* Set the process receiving SIGIO/SIGURG signals to us */
  1832.  
  1833.  
  1834.     if (IoctlSocket(s, FIOSETOWN, &thisTask) < 0) -
  1835.          perror("IoctlSocket FIOSETOWN");
  1836.          exit(1);
  1837.     "
  1838.  
  1839.  
  1840.     /* Allow receipt of asynchronous I/O signals */
  1841.  
  1842.  
  1843.     if (IoctlSocket(s, FIOASYNC, &yes) < 0) -
  1844.          perror("IoctlSocket FIOASYNC");
  1845.          exit(1);
  1846.     "
  1847.  
  1848.  
  1849.  
  1850. 3.4.4   Selecting Specific Protocols
  1851.  
  1852. If the third argument to the socket() call is 0, socket will select a
  1853. default protocol to use with the returned socket of the type requested.
  1854. The default protocol is usually correct,and alternate choices are not
  1855. usually available.  However, when using ``raw'' socketsto communicate
  1856. directly with lower-level protocols or hardware interfaces, the protocol
  1857. argument may be important for setting updemultiplexing.  For example,
  1858. raw sockets in the Internet family may be used to implement a new
  1859. protocol above IP, and the socket will receive packets only for the
  1860. protocol specified.  To obtain a particular protocol one determines the
  1861. protocol number as defined within the communication domain.  For the
  1862. Internet domain one may use one of the library routines discussed in section
  1863. 3.2, such as getprotobyname():
  1864.  
  1865.     #include <sys/types.h>
  1866.     #include <sys/socket.h>
  1867.     #include <netinet/in.h>
  1868.     #include <netdb.h>
  1869.      ...
  1870.     pp = getprotobyname("newtcp");
  1871.     s = socket(AF_INET, SOCK_STREAM, pp->p_proto);
  1872.  
  1873. This would result in a socket s using astream based connection, but with
  1874. protocol type of ``newtcp'' instead of the default ``tcp.''
  1875.  
  1876.  
  1877.  
  1878. 3.4.5   Address  Binding
  1879.  
  1880. As was mentioned in section 3.1, bindingaddresses to sockets in the
  1881. Internet domains can be fairly complex.  As a brief reminder, these
  1882. associations are composed of local and foreign addresses, and local and
  1883. foreign ports.  Port numbers are allocated out of separate spaces, one
  1884. for each system and one for each domainon that system.  Through the
  1885. bind() call, a process may specify halfof an association, the <local
  1886. address, local port> part, while the connect() and accept() calls are
  1887.  
  1888.  
  1889. 56    Section 3.4                  AmiTCP/IP                   System Manual
  1890.  
  1891.  
  1892.  
  1893. used to complete a socket's associationby specifying the <foreign
  1894. address, foreign port> part.  Since the association iscreated in two
  1895. steps the association uniqueness requirement indicated previously could
  1896. be violated unless care is taken.  Further, it is unrealistic to expect user
  1897. programs to always know proper values touse for the local address and local
  1898. port since a host may reside on multiplenetworks and the set of allocated
  1899. port numbers is not directly accessibleto a user.
  1900.    To simplify local address binding in the Internet domain the notion of
  1901. a ``wildcard'' address has been provided.   When an address is specified
  1902. as INADDR_ANY (a manifest constant defined in file netinet/in.h), the
  1903. system interprets the address as ``any valid address''.  For example, to
  1904. bind a specific port number to a socket,but leave the local address
  1905. unspecified, the following code might beused:
  1906.  
  1907.     #include <sys/types.h>
  1908.     #include <netinet/in.h>
  1909.      ...
  1910.     struct sockaddr_in sin;
  1911.      ...
  1912.     s = socket(AF_INET, SOCK_STREAM, 0);
  1913.     sin.sin_family = AF_INET;
  1914.     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  1915.     sin.sin_port = htons(MYPORT);
  1916.     bzero(sin.sin_zero, sizeof(sin.sin_zero));
  1917.     bind(s, (struct sockaddr *) &sin, sizeof (sin));
  1918.  
  1919. Sockets with wildcarded local addressesmay receive messages directed to
  1920. the specified port number, and sent to any of the possible addresses
  1921. assigned to a host.  For example, if a host has addresses 128.32.0.4 and
  1922. 10.0.0.78, and a socket is bound as above, the process will be able to
  1923. accept connection requests which are addressed to 128.32.0.4 or
  1924. 10.0.0.78.  If a server process wished to only allow hosts on a given
  1925. network connect to it, it would bind theaddress of the host on the
  1926. appropriate network.
  1927.    In a similar fashion, a local port may be left unspecified (specified
  1928. as zero), in which case the system willselect an appropriate port number
  1929. for it.  For example, to bind a specific local addressto a socket, but
  1930. to leave the local port number unspecified:
  1931.  
  1932.     hp = gethostbyname(hostname);
  1933.     if (hp == NULL) -
  1934.           ...
  1935.     "
  1936.     bzero(&sin, sizeof(sin));
  1937.     bcopy(hp->h_addr, (char *) sin.sin_addr, hp->h_length);
  1938.     sin.sin_port = htons(0);
  1939.     bind(s, (struct sockaddr *) &sin, sizeof (sin));
  1940.  
  1941. The system selects the local port numberbased on two criteria.  The
  1942. first is that on 4BSD systems, Internetports below IPPORT_RESERVED
  1943.  
  1944. (1024) are reserved for privileged processes13 ; Internet ports above
  1945. ________________________________
  1946.   13All processes in AmigaOS are considered as privileged.
  1947.  
  1948.  
  1949. System Manual                   AmiTCP/IP                   Section 3.4    57
  1950.  
  1951.  
  1952.  
  1953. IPPORT_USERRESERVED (5000) are reserved for non-privileged servers.  The
  1954. second is that the port number is not currently bound to some other
  1955. socket.  In order to find a free Internet port number in the privileged
  1956. range the rresvport() library routine may be used as follows to return a
  1957. stream socket in with a privileged portnumber:
  1958.  
  1959.     int lport = IPPORT_RESERVED - 1;
  1960.     int s;
  1961.     s = rresvport(&lport);
  1962.     if (s < 0) -
  1963.          if (errno== EAGAIN)
  1964.              fprintf(stderr, "socket: all ports in use"n");
  1965.          else
  1966.              perror("rresvport: socket");
  1967.           ...
  1968.     "
  1969.  
  1970. The restriction on allocating ports wasdone to allow processes executing
  1971. in a secure environment to perform authentication based on the
  1972. originating address and port number.  For example, therlogin command
  1973. allows users to log in across a networkwithout being asked for a
  1974. password, if two conditions hold:  First, the name of the system the user
  1975.  
  1976. is logging in from is in the file AmiTCP:db/hosts.equiv14  on the system
  1977. he is logging into (or the system name and the user name are in the
  1978. user's .rhosts file in the user's home directory), and second, that the
  1979. user's rlogin process is coming from a privileged port on the machine
  1980. from which he is logging.  The port number and networkaddress of the
  1981. machine from which the user is logging in can be determined either by the from
  1982. result of the accept() call, or from thegetpeername() call.
  1983.    In certain cases the algorithm used by the system in selecting port
  1984. numbers is unsuitable for an application.   This is because associations
  1985. are created in a two step process.  For example, the Internet file
  1986. transfer protocol, FTP, specifies that data connections must always
  1987. originate from the same local port.  However, duplicateassociations are
  1988. avoided by connecting to different foreign ports.   Inthis situation the
  1989. system would disallow binding the same local address and port number to a
  1990. socket if a previous data connection's socket still existed.  To override
  1991. the default port selection algorithm, anoption call must be performed
  1992. prior to address binding:
  1993.  
  1994.      ...
  1995.      long     on = 1;
  1996.      ...
  1997.     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  1998.     bind(s, (struct sockaddr *) &sin, sizeof (sin));
  1999.  
  2000. With the above call, local addresses maybe bound which are already in
  2001. use.  This does not violate the uniqueness requirementas the system
  2002. still checks at connect time to be sureany other sockets with the same
  2003. local address and port do not have the same foreign address and port.  If
  2004. the association already exists, the error EADDRINUSE is returned.
  2005. ________________________________
  2006.   14In UNIX /etc/hosts.equiv
  2007.  
  2008.  
  2009. 58    Section 3.4                  AmiTCP/IP                   System Manual
  2010.  
  2011.  
  2012.  
  2013. 3.4.6   Broadcasting And Determining Network Configuration
  2014.  
  2015. By using a datagram socket, it is possible to send broadcast packets on
  2016. many networks supported by the system.  The network itself must support
  2017. broadcast; the system provides no simulation of broadcast in software.
  2018. Broadcast messages can place a high loadon a network since they force
  2019. every host on the network to service them.   Consequently, the ability to
  2020. send broadcast packets has been limitedto sockets which are explicitly
  2021. marked as allowing broadcasting.  Broadcast is typically used for one of
  2022. two reasons:  it is desired to find a resource on a local network without
  2023. prior knowledge of its address, or important functions such as routing
  2024. require that information be sent to allaccessible neighbors.
  2025.    To send abroadcast message, a datagram socket should be created:
  2026.  
  2027.     s = socket(AF_INET, SOCK_DGRAM, 0);
  2028.  
  2029. The socket is marked as allowing broadcasting,
  2030.  
  2031.      long     on = 1;
  2032.  
  2033.  
  2034.     setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on));
  2035.  
  2036. and at least a port number should be bound to the socket:
  2037.  
  2038.     sin.sin_family = AF_INET;
  2039.     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  2040.     sin.sin_port = htons(MYPORT);
  2041.     bzero(sin.sin_zero, sizeof(sin.sin_zero));
  2042.     bind(s, (struct sockaddr *) &sin, sizeof (sin));
  2043.  
  2044. The destination address of the message to be broadcast depends on the
  2045. network(s) on which the message is to bebroadcast.   The Internet domain
  2046. supports a shorthand notation for broadcast on the local network, the
  2047. address INADDR_BROADCAST (defined in netinet/in.h).  To determine the
  2048. list of addresses for all reachable neighbors requires knowledge of the
  2049. networks to which the host is connected.  Since this information should
  2050. be obtained in a host independent fashion and may be impossible to
  2051. derive, 4.3BSD provides a method of retrieving this information from the
  2052. system data structures.  The SIOCGIFCONF IoctlSocket()call returns the
  2053. interface configuration of a host in theform of a single ifconf
  2054. structure; this structure contains a ``data area'' which is made up of an array
  2055. of of ifreq structures, one for each network interface to which the host is
  2056. connected.  These structures are defined in net/if.h asfollows:
  2057.  
  2058.     struct ifconf -
  2059.          int ifc_len;      /* size of associated buffer */
  2060.          union -
  2061.              caddr_t         ifcu_buf;
  2062.              struct ifreq *ifcu_req;
  2063.          " ifc_ifcu;
  2064.     ";
  2065.  
  2066.  
  2067.     #define ifc_buf     ifc_ifcu.ifcu_buf/* buffer address */
  2068.     #define ifc_req     ifc_ifcu.ifcu_req/* array of structures returned */
  2069.  
  2070.  
  2071.      #define IFNAMSIZ     64
  2072.  
  2073.  
  2074. System Manual                   AmiTCP/IP                   Section 3.4    59
  2075.  
  2076.  
  2077.  
  2078.     struct ifreq -
  2079.          char   ifr_name[IFNAMSIZ]; /*if name, e.g. "en0" */
  2080.          union -
  2081.              struct sockaddr ifru_addr;
  2082.              struct sockaddr ifru_dstaddr;
  2083.              struct sockaddr ifru_broadaddr;
  2084.              short  ifru_flags;
  2085.              caddr_t         ifru_data;
  2086.          " ifr_ifru;
  2087.     ";
  2088.  
  2089.  
  2090.      #define ifr_addr      ifr_ifru.ifru_addr     /* address */
  2091.     #define ifr_dstaddr   ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
  2092.     #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
  2093.     #define ifr_flags     ifr_ifru.ifru_flags   /* flags */
  2094.      #define ifr_data      ifr_ifru.ifru_data     /* for use by interface */
  2095.  
  2096.    The actual call which obtains the interface configuration is
  2097.  
  2098.     struct ifconf ifc;
  2099.     char buf[BUFSIZ];
  2100.  
  2101.  
  2102.     ifc.ifc_len = sizeof (buf);
  2103.     ifc.ifc_buf = buf;
  2104.     if (IoctlSocket(s, SIOCGIFCONF, (char *) &ifc) < 0) -
  2105.           ...
  2106.     "
  2107.  
  2108. After this call buf will contain one ifreq structure for each network to
  2109. which the host is connected, and ifc.ifc_len will have been modified to
  2110. reflect the number of bytes used by theifreq structures.
  2111.    For eachstructure there exists a set of ``interface flags'' which tell
  2112. whether the network corresponding to that interface is up or down, point
  2113. to point or broadcast, etc.  The SIOCGIFFLAGS IoctlSocket() retrieves
  2114. these flags for an interface specified by an ifreq structure as follows:
  2115.  
  2116.     struct ifreq *ifr;
  2117.  
  2118.  
  2119.     ifr = ifc.ifc_req;
  2120.  
  2121.  
  2122.     for (n = ifc.ifc_len / sizeof (struct ifreq); --n >= 0; ifr++) -
  2123.          /*
  2124.           * We must be careful that we don't use an interface
  2125.           * devoted to anaddress family other than those intended;
  2126.           * if we were interested in NS interfaces, the
  2127.           * AF_INET wouldbe AF_NS.
  2128.           */
  2129.          if (ifr->ifr_addr.sa_family != AF_INET)
  2130.              continue;
  2131.          if (IoctlSocket(s, SIOCGIFFLAGS, (char *) ifr) < 0) -
  2132.               ...
  2133.          "
  2134.          /*
  2135.  
  2136.  
  2137. 60    Section 3.4                  AmiTCP/IP                   System Manual
  2138.  
  2139.  
  2140.  
  2141.           * Skip boring cases.
  2142.           */
  2143.          if ((ifr->ifr_flags & IFF_UP) == 0 __
  2144.              (ifr->ifr_flags & IFF_LOOPBACK) __
  2145.              (ifr->ifr_flags & (IFF_BROADCAST_ IFF_POINTTOPOINT)) == 0)
  2146.              continue;
  2147.  
  2148. Once the flags have been obtained, the broadcast address must be
  2149. obtained.  In the case of broadcast networks this is done via the
  2150. SIOCGIFBRDADDR IoctlSocket(), while forpoint-to-point networks the
  2151. address of the destination host is obtained with SIOCGIFDSTADDR.
  2152.  
  2153.     struct sockaddr dst;
  2154.  
  2155.  
  2156.     if (ifr->ifr_flags & IFF_POINTTOPOINT) -
  2157.          if (IoctlSocket(s, SIOCGIFDSTADDR, (char *) ifr) < 0) -
  2158.               ...
  2159.          "
  2160.          bcopy((char *) ifr->ifr_dstaddr, (char *) &dst,
  2161.                sizeof (ifr->ifr_dstaddr));
  2162.     " else if (ifr->ifr_flags & IFF_BROADCAST) -
  2163.          if (IoctlSocket(s, SIOCGIFBRDADDR, (char *) ifr) < 0) -
  2164.               ...
  2165.          "
  2166.          bcopy((char *) ifr->ifr_broadaddr, (char *) &dst,
  2167.                sizeof (ifr->ifr_broadaddr));
  2168.     "
  2169.  
  2170. After the appropriate ioctl's have obtained the broadcast or destination
  2171. address (now in dst), the sendto() callmay be used:
  2172.  
  2173.          sendto(s,buf, buflen, 0, (struct sockaddr *)&dst, sizeof (dst));
  2174.     "
  2175.  
  2176. In the above loop one sendto() occurs for every interface to which the
  2177. host is connected that supports the notion of broadcast or point-to-point
  2178. addressing.  If a process only wished to send broadcastmessages on a
  2179. given network, code similar to that outlined above would be used, but the
  2180. loop would need to find the correct destination address.
  2181.    Receivedbroadcast messages contain the senders address and port, as
  2182. datagram sockets are bound before a message is allowed to go out.
  2183.  
  2184.  
  2185.  
  2186. AmiTCP/IP specific extensions
  2187.  
  2188. Extensions to interface ioctls
  2189.  
  2190. The following ioctls are used to configure protocol and hardware specific
  2191. properties of a sana_softc interface.  They are used in the AmiTCP/IP
  2192. only.
  2193.  
  2194. SIOCSSANATAGS  Set SANA-II specific properties with a tag list.
  2195.  
  2196. SIOCGSANATAGS  Get SANA-II specific properties into the
  2197.     wiretype_parameters structure and a user tag list.
  2198.  
  2199.     These ioctls use the following structure as a argument:
  2200.  
  2201.  
  2202. System Manual                   AmiTCP/IP                   Section 3.4    61
  2203.  
  2204.  
  2205.  
  2206.          struct wiretype_parameters
  2207.          -
  2208.            ULONG  wiretype;       /* the wiretype of the interface */
  2209.            WORD   flags;                                /* iff_flags */
  2210.            struct TagItem *tags;         /* tag list user provides */
  2211.          ";
  2212.  
  2213.  
  2214. SIOCGARPT  Getthe contents of an ARP mapping cache into a struct arpreq
  2215.     table.
  2216.  
  2217.     This ioctl takes the following arptabreq structure as an argument:
  2218.  
  2219.  
  2220.          /*
  2221.           * An AmiTCP/IP specific ARP table ioctl request
  2222.           */
  2223.          struct arptabreq -
  2224.             struct arpreq atr_arpreq;  /* To identify theinterface */
  2225.             long   atr_size;           /* # of elementsin atr_table */
  2226.             long   atr_inuse;                /* # of elements in use */
  2227.             struct arpreq *atr_table;
  2228.          ";
  2229.  
  2230.  
  2231.     The atr_arpreq specifies the used interface.  The hardware address
  2232.     for the interface is returned in the arp_ha field of atr_arpreq
  2233.     structure.
  2234.  
  2235.     The SIOCGARPT ioctl reads at most atr_size entries from the cache
  2236.     into the user supplied buffer atr_table, if it is not NULL. Actual
  2237.     amount of returned entries is returned in atr_size.  The current
  2238.     amount of cached mappings is returned in the atr_inuse.
  2239.  
  2240.  
  2241.  
  2242. 3.4.7   Socket  Options
  2243.  
  2244. It is possible to set and get a number of options on sockets via the
  2245. setsockopt() and getsockopt() calls.  These options include such things
  2246. as marking a socket for broadcasting, not to route, to linger on close,
  2247. etc.  The general forms of the calls are:
  2248.  
  2249.     setsockopt(s, level, optname, optval, optlen);
  2250.  
  2251. and
  2252.  
  2253.     getsockopt(s, level, optname, optval, optlen);
  2254.  
  2255.    The parameters to the calls are as follows:  s is the socket on which
  2256. the option is to be applied.  level specifies the protocol layer on which
  2257. the option is to be applied; in most cases this is the ``socket level'',
  2258. indicated by the symbolic constant SOL_SOCKET, defined in sys/socket.h.
  2259. The actual option is specified in optname, and is a symbolic constant
  2260. also defined in sys/socket.h.  optval and optlen pointto the value of
  2261. the option (in most cases, whether the option is to be turned on or off),
  2262. and the length of the value of the option, respectively.  For
  2263. getsockopt(), optlen is a value--resultparameter, initially set to the
  2264.  
  2265.  
  2266. 62    Section 3.4                  AmiTCP/IP                   System Manual
  2267.  
  2268.  
  2269.  
  2270. size of the storage area pointed to by optval, and modified upon return
  2271. to indicate the actual amount of storageused.
  2272.    An example should help clarify things.  It is sometimes useful to
  2273. determine the type (e.g., stream, datagram, etc.)   ofan existing socket;
  2274. programs under inetd (described in section 3.4.8) may need to perform
  2275. this task.  This can be accomplished as follows via theSO _TYPE socket
  2276. option and the getsockopt() call:
  2277.  
  2278.  
  2279.     #include <sys/types.h>
  2280.     #include <sys/socket.h>
  2281.  
  2282.  
  2283.     long type, size;
  2284.  
  2285.  
  2286.     size = sizeof (type);
  2287.  
  2288.  
  2289.     if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char *) &type, &size) < 0) -
  2290.           ...
  2291.     "
  2292.  
  2293.  
  2294. After the getsockopt() call, type will be set to the value of the socket
  2295. type, as defined in sys/socket.h.  If, for example, thesocket were a
  2296. datagram socket, type would have the value corresponding to SOCK_DGRAM.
  2297.  
  2298.  
  2299.  
  2300. 3.4.8   Inetd
  2301.  
  2302. One of the daemons provided with AmiTCP/IP is inetd, the so called
  2303. ``internet super--server.''  Inetd is invoked at start-up time, and
  2304. determines the servers, for which it isto listen, from the file
  2305. AmiTCP:db/inetd.conf15.  Once this information has been read and a
  2306. pristine environment created, inetd proceeds to create one socket for
  2307. each service it is to listen for, binding the appropriate port number to
  2308. each socket.
  2309.    Inetd then performs a select() on all these sockets for read
  2310. availability, waiting for somebody wishing a connection to the service
  2311. corresponding to that socket.  Inetd then performs an accept() on the
  2312. socket in question, releases the socketwith a ReleaseSocket() call and
  2313. starts the appropriate server.
  2314.    Servers making use of inetd are considerably simplified, as inetd takes
  2315. care of the majority of the work required in establishing a connection.
  2316. The server invoked by inetd expects thesocket connected to its client to
  2317. be found by calling ObtainSocket().  The client socketID for the server
  2318. is found in a DaemonMessage structure given to the server process.
  2319. Usually the netlib:autoinitd.o module takes care of obtaining the client
  2320. socket into global variable server_socket.  For practical purposes the
  2321. code might assume this socket to be 0.
  2322.    One callwhich may be of interest to individuals writing servers under
  2323. inetd is the getpeername() call, which returns the address of the peer
  2324. (process) connected on the other end ofthe socket.   For example, to log
  2325. the Internet address in ``dot notation''(e.g., ``128.32.0.4'') of a
  2326. ________________________________
  2327.   15In UNIX systems /etc/inetd.conf.
  2328.  
  2329.  
  2330. System Manual                   AmiTCP/IP                   Section 3.5    63
  2331.  
  2332.  
  2333.  
  2334. client connected to a server under inetd, the following code might be
  2335. used:
  2336.  
  2337.     struct sockaddr_in name;
  2338.     int namelen = sizeof (name);
  2339.      ...
  2340.     if (getpeername(0, (struct sockaddr *)&name, &namelen) < 0) -
  2341.          syslog(LOG_ERR, "getpeername: %m");
  2342.          exit(1);
  2343.     " else
  2344.          syslog(LOG_INFO, "Connection from %s", inet_ntoa(name.sin_addr));
  2345.      ...
  2346.  
  2347. While the getpeername() call is especially useful when writing programs
  2348. to run with inetd, it can be used underother circumstances.
  2349.    Sources for a very simple TCP protocol server is included with
  2350. AmiTCP/IP as an example.
  2351.  
  2352.  
  2353.  
  2354. 3.5     Deviation  From  Berkeley  Sockets
  2355.  
  2356.  
  2357. This section discusses the differences between the API of the AmiTCP/IP
  2358. and the 4.3BSD. They are not so numerousas it might seem to, but worth
  2359. taking attention to when porting existing 4.3BSD software to AmiTCP/IP.
  2360.  
  2361.  
  2362.  
  2363. 3.5.1   Opening and Closing the Shared Library
  2364.  
  2365. Since the API is provided as a shared library, it must be opened to be
  2366. able to access the functions it provides.   Note that any two tasks may
  2367. not share a socket library base, since the base contains task specific
  2368. information.
  2369.    AmiTCP/IPdoes resource tracking based on the information stored in a
  2370. library base, so it is essential that the library is closed after use,
  2371. since the resources used by the base aregone if the application exits
  2372. without closing the base.
  2373.    See section 3.1.2 for examples and more discussion on the subject.
  2374.  
  2375.  
  2376.  
  2377. 3.5.2   Naming Conventions of the API Functions
  2378.  
  2379. The API functions which preserve the semantics of the BSD calls are named
  2380. the same as the original functions.  In the name of binary compatibility
  2381. between different C compilers the functions which either take a structure
  2382. as an argument or return a structure asa value had to be changed not to
  2383. do so.  These functions are named differently; all words in these
  2384.  
  2385. function names begin with an upper caseletter.   Inlinefunctions16  are
  2386. provided with the original semantics, however.   This makes it possible to
  2387. keep using the original functions when writing the code and still be
  2388. binary compatible.  The inline functions are mostly trivial; for example
  2389. the select() call is actually an inlinewhich calls WaitSelect() with
  2390. last argument as NULL.
  2391. ________________________________
  2392.   16Or linker stubs in compilers with no inline functions.
  2393.  
  2394.  
  2395. 64    Section 3.5                  AmiTCP/IP                   System Manual
  2396.  
  2397.  
  2398.  
  2399.    This is amatter which should be totally invisible for C users, but
  2400. assembler programmers should take attention and be sure to pass arguments
  2401. as described in the function reference for the non-inline versions.
  2402.  
  2403.  
  2404.  
  2405. 3.5.3   errno
  2406.  
  2407.  
  2408. Unix libraries return error values in aglobal variable named errno.
  2409. Since a shared library cannot know the address of any variables of an
  2410. application, the address of the errno must be explicitly told to the
  2411. AmiTCP/IP. An alternative is to use Errno() call to fetch the error
  2412. value, but since the first method needsno modifications to the existing
  2413. sources (besides calling SetErrnoPtr() once in the beginning), it is the
  2414. preferred method.  Section 3.1.2 contains examples andmore discussion
  2415. about the matter.
  2416.  
  2417.  
  2418.  
  2419. 3.5.4   NewField in the sockaddr Structures
  2420.  
  2421.  
  2422. Since AmiTCP/IP is based on the BSD Net/2 release, it has few
  2423. differences to the 4.3BSD. Most notableone is that the sockaddr and
  2424. sockaddr_in structureshave a new field telling the length of the
  2425. structure.  These are named as sa_len and sin_len respectively.   These
  2426. fields are used by the AmiTCP/IP to determine the real length of the
  2427. address given.
  2428.    In addition the sockaddr_in structure has a field named sin_zero, which
  2429. should be initialized to zero before passed to the AmiTCP/IP, since any
  2430. garbage left there will be used by the routing facility (which obviously
  2431. leads to undesired behaviour).
  2432.  
  2433.  
  2434.  
  2435. 3.5.5   Linger  Time
  2436.  
  2437.  
  2438. The unit of the linger time of a socketis a second.   The Net/2 code
  2439.  
  2440. seemed to use ticks17.
  2441.  
  2442.  
  2443.  
  2444. 3.5.6   Transferring Sockets from a Task to Another
  2445.  
  2446.  
  2447. Since AmigaOS has no fork() call, in which the child process inherits the
  2448. file descriptors, and hence sockets, a mechanism for transferring sockets
  2449. from a task to another must be provided.  This is accomplished by the
  2450. calls ReleaseSocket(), ReleaseCopyOfSocket() and ObtainSocket(), which
  2451. release a socket, a copy of a socket andobtain a released socket,
  2452. respectively.  An id is given to a socket placed in thelist of released
  2453. sockets.  This id can be either unique or be based on aservice number in
  2454. cases where it is irrelevant which instance of the server process for the
  2455. service in question obtains the socket.  If an unique id is used, the
  2456. releasing task is responsible of transferring the id to the obtaining
  2457. task.
  2458. ________________________________
  2459.   17One tick is 1/hz:th of a second, where hz is the frequency of the
  2460.  
  2461. electricity of the wall socket.
  2462.  
  2463.  
  2464. System Manual                   AmiTCP/IP                   Section 3.5    65
  2465.  
  2466.  
  2467.  
  2468.    This feature affects the server processes only, since the clients
  2469. usually create the socket(s) on their own.
  2470.  
  2471.  
  2472.  
  2473. 3.5.7   Ioctl  Differences
  2474.  
  2475. The Unix ioctl() function is renamed asIoctlSocket() in AmiTCP/IP to
  2476. avoid name clashes with some C runtime libraries.
  2477.    Followingsummarizes other differences in the ioctl calls:
  2478.  
  2479.  1. FIOCLEX and FIONCLEX are silently ignored, that is, they are
  2480.     accepted, but have no effect.  Whether sockets should be closed on
  2481.     exec() or not is irrelevant, since AmigaOS has no such feature (see
  2482.     discussion about fork() above).
  2483.  
  2484.  2. FIOSETOWN and SIOCSPGRP take a pointer to a struct Task * as an
  2485.     argument instead of a pointer to a process (or group) id.  Note that
  2486.     if the task in question has not opened the bsdsocket.library the
  2487.     owner of the precess is set to NULL (disabled).  The task pointer is
  2488.     used as the receiver of the asynchronous notification signals if
  2489.     asynchronous notification is enabled.
  2490.  
  2491.  3. FIOGETOWN and SIOCGPGRP take a pointer to a struct Task * as an
  2492.     argument in which the current task pointer of the owner of the socket
  2493.     in question is placed on return.
  2494.  
  2495.  
  2496.  
  2497. 3.5.8   Signal  Handling
  2498.  
  2499. There is a fundamental difference between BSD Unix and Amiga signal
  2500. handling and the system call interface.  In the Unix systems a received
  2501. signal may interrupt a process executinga system call.  If there is a
  2502. signal handler installed, it can be executed before the system call
  2503. returns to the main execution branch with an error code.
  2504.    However,there are some system calls which may not be interrupted.  If
  2505. a Unix process has a negative priority,tsleep() does not wake up until
  2506. the specified condition is met.
  2507.    The interrupted system call does not have any unrecoverable effects,
  2508. the execution of the program may continue after the errno is checked
  2509. against other errors than EINTR.
  2510.    In the AmigaOS, Exec, there are no specific system calls.  All OS
  2511. functions are provided by shared libraries.   There areeither no separate
  2512. kernel and user memory spaces, the one common memory space is shared by
  2513. all processes.  The IO system is based on messages, which are implemented
  2514. as shared memory areas.  When a program receives a message to a port, it
  2515. is delivered a signal associated with the port.
  2516.    While itis possible to use signal handlers with Exec, they are even
  2517. more dangerous to use and restricted than in Unix systems.  This is not
  2518. recommended, since the exception handlermust behave like any real
  2519. interrupt handler.  Calls provided by AmiTCP/IP are notcallable from
  2520. interrupts.  Further, it is not possible to interrupt asystem call
  2521. implemented as a shared library function.
  2522.    The application must itself react on receipt of the signals.  The
  2523. recommended way of handling these signals is by the normal Wait() of by
  2524.  
  2525.  
  2526. 66    Section 3.5                  AmiTCP/IP                   System Manual
  2527.  
  2528.  
  2529.  
  2530. AmiTCP/IP call WaitSelect(), which allows an application to specify a
  2531. signal mask which should abort the selection.   The application then
  2532. checks the received signals and calls appropriate handler for the signal.
  2533.  
  2534.  
  2535.  
  2536. 3.5.9   Asynchronous I/O
  2537.  
  2538.  
  2539. AmigaOS does not have any reserved signals for networking, such as SIGIO
  2540. or SIGURG in Unix systems, and so the scheme used in asynchronous
  2541. notification must be changed a little.
  2542.    The application can set a group of signal masks, with function named
  2543. SetSocketSignals(), to be used by the AmiTCP/IP. First argument
  2544. specifies the signal mask which should break the blocking of the blocking
  2545. socket calls.  It is by default set to the signal for CTRL-C. Second
  2546. argument specifies the signal(s) to be sent when asynchronous
  2547. notification for readiness to read is necessary.   Thismask lets the
  2548. application define which signal should be used as replacement for SIGIO
  2549. signal of the Unix systems.  Third and last argument specifies the
  2550. corresponding mask for the asynchronousnotification of urgent
  2551. (out-of-band) data (SIGURG). These lasttwo masks are zero by default.
  2552.    Note thatthere is no way to query the current settings of these
  2553. signals form the AmiTCP/IP, so the application must store the signal
  2554. numbers (or masks) for later use.  Also note that the break mask must be
  2555. explicitly given if SetSocketSignals() is called, since the values
  2556. supplied override the default settings.
  2557.  
  2558.  
  2559.  
  2560. 3.5.10   Constructing an Event Loop
  2561.  
  2562.  
  2563. Amiga programs are often constructed around an event loop, in which
  2564. Wait() function is used to wait for somesubset of given signals to
  2565. arrive.  When a signal is received, some actions are taken and if IO is
  2566. performed, it is usually asynchronous.
  2567.    Many Unixprograms use to do synchronous IO and let the signal handlers
  2568. to handle special events (window size changes, timeouts, etc.).  This can
  2569. be emulated to some extent with AmigaOS,since it is possible to specify
  2570. an exeption function to handle receptionof given signals.  This is very
  2571. limited, though, since the exeption codeis executed at true interrupt
  2572. level, and may thus pre-empt the main process in an arbitrary location.
  2573. Also note that a very limited set of shared library functions can be
  2574. called while in interrupt, especially note that any AmiTCP/IP function
  2575. may NOT be called from interrupt code.
  2576.    AmiTCP/IPoffers remedy for this, however.  The application can use
  2577. WaitSelect() to handle both Amiga signals and socket IO multiplexing.
  2578.  
  2579. Selecting assures that the following socket calls will not block18.
  2580.    Another possiblility is to use signal driven socket IO (see section
  2581. 3.4.3).
  2582.    Yet another possibility is to specify a special break mask with
  2583. SetSocketSignals() function.  The signals in the mask cause any blocking
  2584. socket IO routine to return with the error code EINTR. Note that the
  2585. ________________________________
  2586.   18See NOTES section of the reference for the WaitSelect().
  2587.  
  2588.  
  2589. System Manual                   AmiTCP/IP                   Section 3.5    67
  2590.  
  2591.  
  2592.  
  2593. signals are not cleared in this procedure.   The Wait()with the same
  2594. signal mask can be used to determine (and clear) the received signals.
  2595. This allows the usage of synchronous socket IO, but the EINTR error code
  2596. must be checked after each failing call.
  2597.  
  2598.  
  2599.  
  2600. 3.5.11   ''Killing'' the Processes
  2601.  
  2602. In AmigaOS the applications must co-operate with the OS for the user to
  2603. be able to stop them.  This is why the blocking operations of the
  2604. AmiTCP/IP can be aborted.  By default the reception ofCTRL-C signal
  2605. aborts any blocking call.  The call returns an error value (in errno) of
  2606. EINTR when aborted.  In addition the signal which caused the break will
  2607. remain set for the application to be able to react on it in its normal
  2608. event processing.  This means that the application neednot specially
  2609. check for EINTR after every socket callas long as they eventually check
  2610. for the break signal.
  2611.    All sockets left open by the application are closed by the
  2612. CloseLibrary() call.  You may left the sockets open when aborting the
  2613. program, because the socket library is closed automatically during the
  2614. exit process if either autotermination function (specific to SAS C) or
  2615. ANSI atexit() function is installed before the exit is done.  .
  2616.    The signals which cause the abort can be set with the
  2617. SetSocketSignals() call.  The break signal mask is given as the first
  2618. argument.  Calling this function discards the previousvalues of the
  2619. sockets signal masks.  Aborting can be disabled by giving the mask as 0L.
  2620. See section 3.5.9 for more discussion about the SetSocketSignals() call.
  2621.  
  2622.  
  2623.  
  2624. 3.5.12   WaitSelect()
  2625.  
  2626. In AmiTCP/IP no other than socket I/O can be multiplexed with the
  2627. select() call.  This may be a major pain as I/O is normally multiplexed
  2628. with an Wait() loop, waiting for given signals to arrive.  This is the
  2629. motivation for the WaitSelect() call.  It combines theselection and
  2630. waiting in a single call19.  The WaitSelect() takes one argument in
  2631. addition to the normal select() call.  It is a pointerto signal mask to
  2632. wait for in addition to the signals thatthe AmiTCP/IP uses internally.
  2633. If any of these signals is received, they are returned as a result in the
  2634. same signal mask.  Signals specified in the given signal mask override
  2635. the signals of the break mask (see previous section).  If the same signal
  2636. is specified in both the SIGINTR mask and the mask given to the WaitSelect(),
  2637. the reception of the signal causes it tobe cleared and returned in the mask
  2638. as the result.
  2639.    WaitSelect() can be used as replacement for the Wait() in applications
  2640. which require to multiplex both socket related and other Amiga I/O.
  2641.  
  2642.  
  2643.  
  2644. ________________________________
  2645.   19This feature was really easy to implement, since AmiTCP/IP uses a Wait()
  2646.  
  2647. to wait for I/O events itself.
  2648.  
  2649.  
  2650. 68    Section 3.5                  AmiTCP/IP                   System Manual
  2651.  
  2652.  
  2653.  
  2654.  
  2655.  
  2656. Appendix B
  2657.  
  2658.  
  2659.  
  2660. API  Function  Reference
  2661.  
  2662.  
  2663.  
  2664. This appendix is a complete reference tothe functions and concepts
  2665. provided by the AmiTCP/IP system.
  2666.  
  2667.  
  2668.  
  2669. Table  of  Contents
  2670.  
  2671.  
  2672.  
  2673.    Standard BSD Style Socket Functions
  2674.         accept  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2675.  *: : : : : : : : : 130
  2676.         bind : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : :*
  2677.  * : : : : : : : : : 132
  2678.         CloseSocket : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2679.  *: : : : : : : : 133
  2680.         connect : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2681.  *: : : : : : : : : 134
  2682.         Dup2Socket  : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : *
  2683.  *: : : : : : : : 136
  2684.         getpeername : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2685.  *: : : : : : : : 137
  2686.         getsockname : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2687.  *: : : : : : : : 138
  2688.         getsockopt  : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : *
  2689.  *: : : : : : : : 139
  2690.         IoctlSocket : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2691.  *: : : : : : : : 142
  2692.         listen  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2693.  *: : : : : : : : : 144
  2694.         recv : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : :*
  2695.  * : : : : : : : : : 145
  2696.         recvfrom : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  2697.  * : : : : : : : : 145
  2698.         select  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2699.  *: : : : : : : : : 147
  2700.         send : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : :*
  2701.  * : : : : : : : : : 150
  2702.         sendto  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2703.  *: : : : : : : : : 150
  2704.         setsockopt  : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : *
  2705.  *: : : : : : : : 139
  2706.         shutdown : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  2707.  * : : : : : : : : 152
  2708.         socket  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2709.  *: : : : : : : : : 153
  2710.         WaitSelect  : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : *
  2711.  *: : : : : : : : 147
  2712.  
  2713.  
  2714.    Other BSD Functions Related to Sockets
  2715.         getdtablesize : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2716.  * : : : :: : : 156
  2717.         Syslog  : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : *
  2718.  *: : : : : : : : : 157
  2719.  
  2720.  
  2721.    Network Data and Address Manipulation
  2722.         inet_addr : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2723.  * : : : : : : :: 159
  2724.         Inet_LnaOf: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2725.  * : : : :: : : : 159
  2726.         inet_lnaof: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2727.  * : : : :: : : : 159
  2728.  
  2729.  
  2730.  
  2731.                                       128
  2732.  
  2733.  
  2734. System Manual                  AmiTCP/IP                 Section B.0    129
  2735.  
  2736.  
  2737.  
  2738.         inet_MakeAddr : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2739.  * : : : : : : :159
  2740.         inet_makeaddr : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2741.  * : : : : : : :159
  2742.         Inet_NetOf: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2743.  * : : : :: : : : 159
  2744.         inet_netof: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2745.  * : : : :: : : : 159
  2746.         inet_network : : : : : : : : : : : : : : : : : : : : : : :: : : : : : :*
  2747.  * : : : : : : : 159
  2748.         Inet_NtoA : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2749.  * : : : : : : :: 159
  2750.         inet_ntoa : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2751.  * : : : : : : :: 159
  2752.  
  2753.  
  2754.    Network, Protocol and Service Queries
  2755.         gethostbyaddr : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2756.  * : : : :: : : 162
  2757.         gethostbyname : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2758.  * : : : :: : : 162
  2759.         getnetbyaddr : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  2760.  * : : : : : : : 164
  2761.         getnetbyname : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  2762.  * : : : : : : : 164
  2763.         getprotobyname  : : : : : : :: : : : : : : : : : : : : : : : : : : : : *
  2764.  *: : : : : : : 166
  2765.         getprotobynumber : : : : : : : : : : : : : : : : : :: : : : : : : : : :*
  2766.  * : : : : : : 166
  2767.         getservbyname : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2768.  * : : : :: : : 167
  2769.         getservbyport : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2770.  * : : : :: : : 167
  2771.  
  2772.  
  2773.    AmiTCP/IP Specific Extensions
  2774.         Errno : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2775.  * : : :: : : : : : 169
  2776.         ObtainSocket : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  2777.  * : : : : : : : 170
  2778.         ReleaseCopyOfSocket : :: : : : : : : : : : : : : : : : : : : : : : : : *
  2779.  *: : : : : : 171
  2780.         ReleaseSocket : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2781.  * : : : :: : : 172
  2782.         SetDTableSize : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  2783.  * : : : :: : : 173
  2784.         SetErrnoPtr : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  2785.  *: : : : : : : : 174
  2786.         SetSocketSignals : : : : : : : : : : : : : : : : : :: : : : : : : : : :*
  2787.  * : : : : : : 175
  2788.  
  2789.  
  2790. 130    Section B.1                 AmiTCP/IP                  System Manual
  2791.  
  2792.  
  2793.  
  2794. B.1     Standard  BSD  Style Socket  Functions
  2795.  
  2796.  
  2797.  
  2798. B.1.1   accept()
  2799.  
  2800.  
  2801.    NAME
  2802.          accept -accept a connection on a socket
  2803.  
  2804.  
  2805.    SYNOPSIS
  2806.          #include <sys/types.h>
  2807.          #include <sys/socket.h>
  2808.  
  2809.  
  2810.          ns = accept(s, addr, addrlen)
  2811.          D0          D0 A0     A1
  2812.  
  2813.  
  2814.          long accept(long, struct sockaddr *, long *);
  2815.  
  2816.  
  2817.    FUNCTION
  2818.          The  argument  s  is a  socket  that  has  been created with
  2819.          socket(),bound to an  address  with bind(), and  is listen-
  2820.          ing for connections after a listen().  accept() extracts the
  2821.          first  connection  on  the  queue  of  pending  connections,
  2822.          creates anew socket with the same properties of s and allo-
  2823.          cates a new socket descriptor for the socket.  If no pending
  2824.          connections are present on the  queue, and the socket is not
  2825.          marked  as non-blocking, accept() blocks the caller  until a
  2826.          connection is present.  If the socket is marked non-blocking
  2827.          and  no pending  connections  are  present  on  the  queue,
  2828.          accept()returns  an error as described below.  The accepted
  2829.          socket isused to read and write data to and from the socket
  2830.          which connected to  this one; it is not used to  accept more
  2831.          connections.   The original socket s remains open for accept-
  2832.          ing further connections.
  2833.  
  2834.  
  2835.          The argument addr is a result parameter that  is  filled  in
  2836.          with  the address of the connecting entity, as known to the
  2837.          communications layer.  The exact format of the addr  parame-
  2838.          ter  is determined by the domain in which the communication
  2839.          is occurring.  The addrlen is a value-result  parameter;  it
  2840.          should  initially  contain the amount of space pointed to by
  2841.          addr; onreturn it will contain the actual length (in bytes)
  2842.          of    the   address    returned.    This  call  is  used  with
  2843.          connection-based socket types, currently with SOCK_STREAM.
  2844.  
  2845.  
  2846.          It is possible to select() a socket  for  the  purposes  of
  2847.          doing anaccept() by selecting it for read.
  2848.  
  2849.  
  2850.    RETURN VALUES
  2851.          accept()returns a non-negative descriptor for the  accepted
  2852.          socket onsuccess.   Onfailure, it returns -1 and sets errno
  2853.          to indicate the error.
  2854.  
  2855.  
  2856. System Manual                  AmiTCP/IP                 Section B.1    131
  2857.  
  2858.  
  2859.  
  2860.    ERRORS
  2861.          EBADF         - The descriptoris invalid.
  2862.  
  2863.  
  2864.          EINTR         - The operation was interrupted by a break
  2865.                          signal.
  2866.  
  2867.  
  2868.          EOPNOTSUPP    - The referencedsocket is not of type
  2869.                          SOCK_STREAM.
  2870.  
  2871.  
  2872.          EWOULDBLOCK   - The socket is marked non-blocking and no con-
  2873.                          nections are present to be accepted.
  2874.  
  2875.  
  2876.    SEE ALSO
  2877.          bind(), connect(), listen(), select(), SetSocketSignals(),
  2878.          socket()
  2879.  
  2880.  
  2881. 132    Section B.1                 AmiTCP/IP                  System Manual
  2882.  
  2883.  
  2884.  
  2885. B.1.2   bind()
  2886.  
  2887.    NAME
  2888.          bind - bind a name to a socket
  2889.  
  2890.  
  2891.    SYNOPSIS
  2892.          #include <sys/types.h>
  2893.          #include <sys/socket.h>
  2894.  
  2895.  
  2896.          success =bind(s, name, namelen)
  2897.          D0              D0 A0    D1
  2898.  
  2899.  
  2900.          long bind(long, struct sockaddr *, long);
  2901.  
  2902.  
  2903.    FUNCTION
  2904.          bind() assigns a name to an unnamed socket.  When  a  socket
  2905.          is created with socket(2) it exists in a name space (address
  2906.          family) but has no name assigned.  bind() requests that  the
  2907.          name pointed to by name be assigned to the socket.
  2908.  
  2909.  
  2910.    RETURN VALUES
  2911.          0  - on success.
  2912.  
  2913.  
  2914.          -1 - on failure and sets errno to indicate the error.
  2915.  
  2916.  
  2917.    ERRORS
  2918.          EACCES            - The requested address is protected,  and
  2919.                               the  current user has inadequate permis-
  2920.                               sion to access it.
  2921.  
  2922.  
  2923.          EADDRINUSE         - The specified address is already in use.
  2924.  
  2925.  
  2926.          EADDRNOTAVAIL      - The specified address is  not  available
  2927.                               from the local machine.
  2928.  
  2929.  
  2930.          EBADF              -s is not a valid descriptor.
  2931.  
  2932.  
  2933.          EINVAL            - namelen is  not  the  size  of  a  valid
  2934.                               address  for  the specified address fam-
  2935.                               ily.
  2936.  
  2937.  
  2938.                               The  socket  is  already  bound  to   an
  2939.                               address.
  2940.  
  2941.  
  2942.    SEE ALSO
  2943.          connect(), getsockname(), listen(), socket()
  2944.  
  2945.  
  2946.    NOTES
  2947.          The rulesused in name binding  vary  between  communication
  2948.          domains.
  2949.  
  2950.  
  2951. System Manual                  AmiTCP/IP                 Section B.1    133
  2952.  
  2953.  
  2954.  
  2955. B.1.3   CloseSocket()
  2956.  
  2957.    NAME
  2958.          CloseSocket - delete a socket descriptor
  2959.  
  2960.  
  2961.    SYNOPSIS
  2962.          success =CloseSocket(s)
  2963.          D0                     D0
  2964.  
  2965.  
  2966.          long CloseSocket(long);
  2967.  
  2968.  
  2969.    FUNCTION
  2970.          CloseSocket() deletes  a  descriptor  from the  library base
  2971.          socket reference table.    Ifs is the last reference  to the
  2972.          underlying object, then the object  will  be deactivated and
  2973.          socket  (see socket()),  associated naming  information  and
  2974.          queued data are discarded.
  2975.  
  2976.  
  2977.          All sockets are automatically closed when the socket library
  2978.          is closed, but closing sockets as soon as possible is
  2979.          recommended to save system resources.
  2980.  
  2981.  
  2982.    RETURN VALUES
  2983.           0   on success.
  2984.  
  2985.  
  2986.          -1   on failure and sets errnoto indicate the error.
  2987.  
  2988.  
  2989.    ERRORS
  2990.          EBADF              -s is not an active socket descriptor.
  2991.  
  2992.  
  2993.          EINTR              -linger on close was interrupted.
  2994.                               The socket is closed, however.
  2995.  
  2996.  
  2997.    SEE ALSO
  2998.          accept(),SetSocketSignals(), shutdown(), socket(),
  2999.          exec.library/CloseLibrary()
  3000.  
  3001.  
  3002. 134    Section B.1                 AmiTCP/IP                  System Manual
  3003.  
  3004.  
  3005.  
  3006. B.1.4   connect()
  3007.  
  3008.  
  3009.  
  3010.    NAME
  3011.          connect -initiate a connection on a socket
  3012.  
  3013.  
  3014.    SYNOPSIS
  3015.          #include <sys/types.h>
  3016.          #include <sys/socket.h>
  3017.  
  3018.  
  3019.          success =connect(s, name, namelen)
  3020.          D0                 D0 A0    D1
  3021.  
  3022.  
  3023.          long connect(long, struct sockaddr *, long);
  3024.  
  3025.  
  3026.    FUNCTION
  3027.          The parameter s is a socket.  If it  is of  type SOCK_DGRAM,
  3028.          then  this call specifies the peer with which the  socket is
  3029.          to be associated;  this address  is that  to which datagrams
  3030.          are  to be sent, and  the only address from which  datagrams
  3031.          are to bereceived.   Ifit is of type SOCK_STREAM, then this
  3032.          call attempts  to make a connection  to another socket.  The
  3033.          other socket is specified by name which is an address in the
  3034.          communications space of  the  socket.   Each  communications
  3035.          space interprets the  name parameter in  its  own way.  Gen-
  3036.          erally, stream sockets may successfully connect() only once;
  3037.          datagramsockets may use connect() multiple  times to change
  3038.          their association.  Datagram sockets may dissolve the  asso-
  3039.          ciation by connecting to  an invalid address, such as a null
  3040.          address.
  3041.  
  3042.  
  3043.    RETURN VALUES
  3044.           0   on success.
  3045.  
  3046.  
  3047.          -1   on failure and sets errnoto indicate the error.
  3048.  
  3049.  
  3050.    ERRORS
  3051.          EADDRINUSE         - The address is already in use.
  3052.  
  3053.  
  3054.          EADDRNOTAVAIL      - The specified address is  not  available
  3055.                               on the remote machine.
  3056.  
  3057.  
  3058.          EAFNOSUPPORT       - Addresses in the specified address   fam-
  3059.                               ily cannot be used with this socket.
  3060.  
  3061.  
  3062.          EALREADY          - The socket is non-blocking and a  previ-
  3063.                               ous  connection attempt has not yet been
  3064.                               completed.
  3065.  
  3066.  
  3067.          EBADF              -s is not a valid descriptor.
  3068.  
  3069.  
  3070.          ECONNREFUSED       - The attempt to  connect  was forcefully
  3071.  
  3072.  
  3073. System Manual                  AmiTCP/IP                 Section B.1    135
  3074.  
  3075.  
  3076.  
  3077.                               rejected.   The  calling  program should
  3078.                               CloseSocket() the socket descriptor, and
  3079.                               issue another socket()  call to obtain a
  3080.                               new descriptor before attempting another
  3081.                               connect() call.
  3082.  
  3083.  
  3084.          EINPROGRESS        - The socket is non-blocking and the  con-
  3085.                               nection cannot be completed immediately.
  3086.                               It is possible to select()  for  comple-
  3087.                               tion  by  selecting the socket for writ-
  3088.                               ing.
  3089.  
  3090.  
  3091.          EINTR              -The operation was interrupted by a break
  3092.                               signal.
  3093.  
  3094.  
  3095.          EINVAL            - namelen is  not  the  size  of  a  valid
  3096.                               address  for  the specified address fam-
  3097.                               ily.
  3098.  
  3099.  
  3100.          EISCONN             The socket is already connected.
  3101.  
  3102.  
  3103.          ENETUNREACH        - The network is not reachable  from  this
  3104.                               host.
  3105.  
  3106.  
  3107.          ETIMEDOUT         - Connection    establishment    timed    out
  3108.                               without establishing a connection.
  3109.  
  3110.  
  3111.    SEE ALSO
  3112.          accept(),CloseSocket(), connect(), getsockname(), select(),
  3113.          socket()
  3114.  
  3115.  
  3116. 136    Section B.1                 AmiTCP/IP                  System Manual
  3117.  
  3118.  
  3119.  
  3120. B.1.5   Dup2Socket()
  3121.  
  3122.    NAME
  3123.         Dup2Socket - duplicate a socket descriptor
  3124.  
  3125.  
  3126.    SYNOPSIS
  3127.  
  3128.  
  3129.         newfd = Dup2Socket(fd1, fd2)
  3130.         D0                  D0   D1
  3131.  
  3132.  
  3133.         long Dup2Socket(long, long);
  3134.  
  3135.  
  3136.    DESCRIPTION
  3137.         Dup2Socket() duplicates an existing socket descriptor.
  3138.         theargument fd1 is small non-negative value that indexes
  3139.         thesocket on SocketBase descriptor table. The value must
  3140.         beless than the size of the table, which is returned by
  3141.         getdtablesize(). fd2 specifies the desired value of the new
  3142.         descriptor. If descriptor fd2 is already in use, it is
  3143.         first deallocated as if it were closed by CloseSocket(). If
  3144.         thevalue if fd2 is -1, the new descriptor used and returned
  3145.         isthe lowest numbered descriptor that is not currently in
  3146.         useby the SocketBase.
  3147.  
  3148.  
  3149.    RETURN VALUES
  3150.         Dup2Socket() returns a new descriptor on  success. On failure
  3151.         -1is returned and errno is set to indicate the error.
  3152.  
  3153.  
  3154.    ERRORS
  3155.         EBADF           fd1 or fd2 is not a valid active descriptor.
  3156.  
  3157.  
  3158.         EMFILE          Too many descriptors are active.
  3159.  
  3160.  
  3161.    SEE ALSO
  3162.         accept(), CloseSocket(), getdtablesize(), SetDtableSize(),
  3163.         socket()
  3164.  
  3165.  
  3166. System Manual                  AmiTCP/IP                 Section B.1    137
  3167.  
  3168.  
  3169.  
  3170. B.1.6   getpeername()
  3171.  
  3172.    NAME
  3173.          getpeername - get name of connected peer
  3174.  
  3175.  
  3176.    SYNOPSIS
  3177.          success =  getpeername(s, name, namelen)
  3178.          D0                      D0 A0    A1
  3179.  
  3180.  
  3181.          long getpeername(long, struct sockaddr *, long *);
  3182.  
  3183.  
  3184.    FUNCTION
  3185.          getpeername() returns the name  of  the  peer  connected  to
  3186.          socket  s.    The long  pointed  to  by the namelen parameter
  3187.          should be  initialized  to  indicate  the  amount  of  space
  3188.          pointed to  by name.  On return it contains the actual size
  3189.          of the name returned (in bytes).  The name is  truncated  if
  3190.          the buffer provided is too small.
  3191.  
  3192.  
  3193.    RETURN VALUE
  3194.          A 0 is returned if the call succeeds, -1 if it fails.
  3195.  
  3196.  
  3197.    ERRORS
  3198.          EBADF         - The argument sis not a valid descriptor.
  3199.  
  3200.  
  3201.          ENOBUFS      - Insufficient resources were available in   the
  3202.                          system to perform the operation.
  3203.  
  3204.  
  3205.          ENOTCONN     - The socket is not connected.
  3206.  
  3207.  
  3208.    SEE ALSO
  3209.          accept(),bind(), getsockname(), socket()
  3210.  
  3211.  
  3212. 138    Section B.1                 AmiTCP/IP                  System Manual
  3213.  
  3214.  
  3215.  
  3216. B.1.7   getsockname()
  3217.  
  3218.    NAME
  3219.          getsockname - get socket name
  3220.  
  3221.  
  3222.    SYNOPSIS
  3223.  
  3224.  
  3225.          success =getsockname(s, name, namelen)
  3226.          D0                     D0 A0    A1
  3227.  
  3228.  
  3229.          long getsockname(long, struct sockaddr *, long *);
  3230.  
  3231.  
  3232.    FUNCTION
  3233.          getsockname() returns the current  name  for  the  specified
  3234.          socket.   The  namelen  parameter  should  be initialized to
  3235.          indicatethe amount of space pointed to by name.  On  return
  3236.          it contains the actual size of the name returned (in bytes).
  3237.  
  3238.  
  3239.    DIAGNOSTICS
  3240.          A 0 is returned if the call succeeds, -1 if it fails.
  3241.  
  3242.  
  3243.    ERRORS
  3244.          The callsucceeds unless:
  3245.  
  3246.  
  3247.          EBADF          s is not a valid descriptor.
  3248.  
  3249.  
  3250.          ENOBUFS         Insufficient resources were available in  the
  3251.                          system to perform the operation.
  3252.  
  3253.  
  3254.    SEE ALSO
  3255.          bind(), getpeername(), socket()
  3256.  
  3257.  
  3258. System Manual                  AmiTCP/IP                 Section B.1    139
  3259.  
  3260.  
  3261.  
  3262. B.1.8   getsockopt()
  3263.  
  3264.  
  3265.  
  3266.    NAME
  3267.          getsockopt, setsockopt - get and set options on sockets
  3268.  
  3269.  
  3270.    SYNOPSIS
  3271.          #include <sys/types.h>
  3272.          #include <sys/socket.h>
  3273.  
  3274.  
  3275.          success =  getsockopt(s, level, optname, optval, optlen)
  3276.          D0                     D0 D1     D2        A0      A1
  3277.  
  3278.  
  3279.          long getsockopt(long, long, long, caddr_t, long *);
  3280.  
  3281.  
  3282.          success =  setsockopt(s, level, optname, optval, optlen)
  3283.          D0                     D0 D1     D2        A0      D3
  3284.  
  3285.  
  3286.          long setsockopt(long, long, long, caddr_t, long);
  3287.  
  3288.  
  3289.    FUNCTION
  3290.          getsockopt() and setsockopt() manipulate options  associated
  3291.          with  a socket.  Options may exist at multiple protocol lev-
  3292.          els; theyare always present  at  the  uppermost  ``socket''
  3293.          level.
  3294.  
  3295.  
  3296.          When manipulating socket options  the  level  at  which  the
  3297.          option resides and the name of the option must be specified.
  3298.          To manipulate options at  the  ``socket''  level,  level  is
  3299.          specifiedas SOL_SOCKET.  To manipulate options at any other
  3300.          level theprotocol number of the appropriate  protocol  con-
  3301.          trolling the  option is supplied.  For example, to indicate
  3302.          that an option is to be interpreted  by  the  TCP  protocol,
  3303.          level  should  be  set  to  the  protocol number of TCP.
  3304.  
  3305.  
  3306.          The parameters optval and optlen are used to  access  option
  3307.          values  for  setsockopt().  For getsockopt() they identify a
  3308.          buffer inwhich the value for the requested option(s) are to
  3309.          be  returned.    For  getsockopt(),  optlen is a value-result
  3310.          parameter,  initially  containing  the  size  of  the  buffer
  3311.          pointed to by optval, and modified on return to indicate the
  3312.          actual size of the value returned.  If no option value is to
  3313.          be supplied or returned, optval may be supplied as 0.
  3314.  
  3315.  
  3316.          optname and any specified options are  passed  uninterpreted
  3317.          to  the appropriate protocol module for interpretation.  The
  3318.          include file  <sys/socket.h>    contains    definitions    for
  3319.          ``socket'' level options, described below.  Options at other
  3320.          protocol levels  vary  in  format  and  name.
  3321.  
  3322.  
  3323.          Most socket-level options take an int parameter for  optval.
  3324.          For setsockopt(), the parameter should be non-zero to enable
  3325.  
  3326.  
  3327. 140    Section B.1                 AmiTCP/IP                  System Manual
  3328.  
  3329.  
  3330.  
  3331.          a booleanoption, or zero if the option is to  be  disabled.
  3332.  
  3333.  
  3334.          SO_LINGER   uses  a  struct  linger  parameter,  defined  in
  3335.          <sys/socket.h>, which specifies the  desired  state  of  the
  3336.          option and the linger interval (see below).
  3337.  
  3338.  
  3339.          The following options are recognized at  the  socket  level.
  3340.          Except  as noted, eachmay be examined with getsockopt() and
  3341.          set withsetsockopt().
  3342.  
  3343.  
  3344.               SO_DEBUG           - toggle    recording    of    debugging
  3345.                                    information
  3346.               SO_REUSEADDR      - toggle local address reuse
  3347.               SO_KEEPALIVE      - toggle keep connections alive
  3348.               SO_DONTROUTE      - toggle routing bypass for  outgoing
  3349.                                    messages
  3350.               SO_LINGER          - linger on close if data present
  3351.               SO_BROADCAST      - toggle    permission    to    transmit
  3352.                                    broadcast messages
  3353.               SO_OOBINLINE      - toggle  reception  of   out-of-band
  3354.                                    data in band
  3355.               SO_SNDBUF          - set buffer size for output
  3356.               SO_RCVBUF          - set buffer size for input
  3357.               SO_TYPE            - get the type  of the  socket  (get
  3358.                                    only)
  3359.               SO_ERROR           - get and clear error on  the  socket
  3360.                                    (get  only)
  3361.  
  3362.  
  3363.          SO_DEBUG enables  debugging  in  the  underlying   protocol
  3364.          modules.   SO_REUSEADDR  indicates  that  the  rules used in
  3365.          validating addresses supplied in a bind() call should allow
  3366.          reuse oflocal addresses.  SO_KEEPALIVE enables the periodic
  3367.          transmission of messages on a connected socket.  Should  the
  3368.          connected  party fail to respond to these messages, the con-
  3369.          nection is considered broken. If  the  process  is
  3370.          waiting in select() when the connection is broken, select()
  3371.          returns true for any read or write events selected  for  the
  3372.          socket.    SO_DONTROUTE  indicates  that  outgoing  messages
  3373.          should bypass the  standard  routing  facilities.   Instead,
  3374.          messages are  directed to the appropriate network interface
  3375.          accordingto the network portion of the destination address.
  3376.  
  3377.  
  3378.          SO_LINGERcontrols the action taken when unsent messags  are
  3379.          queued  on  socket and a CloseSocket() is performed.  If the
  3380.          socket promises reliable delivery of data and  SO_LINGER  is
  3381.          set,  the system  will  block  the  process  on the close
  3382.          attempt until it is able to transmit the data  or  until  it
  3383.          decides it  is unable to deliver the information (a timeout
  3384.          period, in seconds, termed the linger interval, is specified
  3385.          in the set- sockopt() call when SO_LINGER is requested).  If
  3386.          SO_LINGER is  disabled and a CloseSocket()  is  issued, the
  3387.  
  3388.  
  3389. System Manual                  AmiTCP/IP                 Section B.1    141
  3390.  
  3391.  
  3392.  
  3393.          system will process the  close  in  a manner that allows the
  3394.          process to continue as quickly as possible.
  3395.  
  3396.  
  3397.          The option SO_BROADCAST requests permission to  send  broad-
  3398.          cast  datagrams  on  the socket.  Broadcast was a privileged
  3399.          operationin earlier versions of the system.  With protocols
  3400.          that  support  out-of-band  data,  the  SO_OOBINLINE  option
  3401.          requeststhat out-of-band data be placed in the normal  data
  3402.          input  queue  as  received;  it will then be accessible with
  3403.          recv() orread() calls without the MSG_OOB flag.   SO_SNDBUF
  3404.          and  SO_RCVBUF are options to adjust the normal buffer sizes
  3405.          allocatedfor output and input buffers,  respectively.   The
  3406.          buffer size may be increased for high-volume connections, or
  3407.          may be decreased to limit the possible backlog  of  incoming
  3408.          data.   The system places an absolute limit on these values.
  3409.          Finally,SO_TYPE and SO_ERROR are  options  used  only  with
  3410.          getsockopt().    SO_TYPE returns the type of the socket, such
  3411.          as SOCK_STREAM; it is useful for servers that inherit  sock-
  3412.          ets  on startup.  SO_ERROR returns any pending error on the
  3413.          socket and clears the error status.  It may be used to check
  3414.          for asynchronous errors on connected datagram sockets or for
  3415.          other asynchronous errors.
  3416.  
  3417.  
  3418.    RETURN VALUES
  3419.           0 - on success.
  3420.  
  3421.  
  3422.          -1 - on failure and set errno to indicate the error.
  3423.  
  3424.  
  3425.    ERRORS
  3426.          EBADF              -s is not a valid descriptor.
  3427.  
  3428.  
  3429.          ENOPROTOOPT        - The option is unknown at the level indi-
  3430.                               cated.
  3431.  
  3432.  
  3433.    SEE ALSO
  3434.          IoctlSocket(), socket()
  3435.  
  3436.  
  3437.    BUGS
  3438.          Several of the socket options should  be  handled  at  lower
  3439.          levels ofthe system.
  3440.  
  3441.  
  3442. 142    Section B.1                 AmiTCP/IP                  System Manual
  3443.  
  3444.  
  3445.  
  3446. B.1.9   IoctlSocket()
  3447.  
  3448.  
  3449.  
  3450.    NAME
  3451.          IoctlSocket - control sockets
  3452.  
  3453.  
  3454.    SYNOPSIS
  3455.  
  3456.  
  3457.          #include <sys/types.h>
  3458.          #include <sys/ioctl.h>
  3459.  
  3460.  
  3461.          value = IoctlSocket(fd, request, arg)
  3462.          D0            D0   D1       A0
  3463.  
  3464.  
  3465.          long IoctlSocket(long, long, caddr_t);
  3466.  
  3467.  
  3468.    FUNCTION
  3469.          IoctlSocket() performs a special function on the object referred
  3470.          to by theopen   socketdescriptor fd. Note: the setsockopt()
  3471.          call (seegetsockopt()) is the primary  method for operating
  3472.          on sockets  as such, rather than on the underlying  protocol
  3473.          or network interface.
  3474.  
  3475.  
  3476.          For mostIoctlSocket() functions, arg is a pointer to data to
  3477.          be used by the  function  or to be filled in by the function.
  3478.          Other functions may ignore arg or may treat it directly as a
  3479.          data item; they may, for example, be passed an int value.
  3480.  
  3481.  
  3482.          The following requests are supported:
  3483.  
  3484.  
  3485.  
  3486.          FIOASYNC            The argument is a  pointer  to  a  long.
  3487.                               Set  or  clear asynchronous I/O.  If the
  3488.                               value of that  long is  a  1  (one)  the
  3489.                               descriptor  is set for asynchronous I/O.
  3490.                               If the value of that long is a  0 (zero)
  3491.                               the  descriptor is cleared for asynchro-
  3492.                               nous I/O.
  3493.  
  3494.  
  3495.          FIOCLEX
  3496.          FIONCLEX            Ignored, no use for close-on-exec flag
  3497.                               in Amiga.
  3498.  
  3499.  
  3500.          FIOGETOWN
  3501.          SIOCGPGRP           The argument is pointer to struct Task*.
  3502.                               Set the value of that pointer to the
  3503.                               Task that  is  receiving SIGIO or SIGURG
  3504.                               signals for  the  socket  referred to by
  3505.                               the descriptor passed to IoctlSocket().
  3506.  
  3507.  
  3508.          FIONBIO             The argument is a  pointer  to  a  long.
  3509.                               Set  or  clear non-blocking I/O.  If the
  3510.  
  3511.  
  3512. System Manual                  AmiTCP/IP                 Section B.1    143
  3513.  
  3514.  
  3515.  
  3516.                               value of that  long is  a  1  (one)  the
  3517.                               descriptor  is set for non-blocking I/O.
  3518.                               If the value of that long is a  0 (zero)
  3519.                               the   descriptor  is  cleared  for  non-
  3520.                               blocking I/O.
  3521.  
  3522.  
  3523.          FIONREAD            The argument is a  pointer  to  a  long.
  3524.                               Set the value of that long to the number
  3525.                               of immediately readable characters  from
  3526.                               the socket fd.
  3527.  
  3528.  
  3529.          FIOSETOWN
  3530.          SIOCSPGRP           The argument is pointer to struct Task*,
  3531.                               pointer  to  the task  that will subseq-
  3532.                               uently receive  SIGIO or  SIGURG signals
  3533.                               for   the  socket  referred  to  by  the
  3534.                               descriptor passed.
  3535.  
  3536.  
  3537.          SIOCCATMARK          The argument is a pointer to a long.
  3538.                               Set the value of that long to 1 if the
  3539.                               read pointer for the socket referred to
  3540.                               by the descriptor passed to
  3541.                               IoctlSocket() points to a mark in the
  3542.                               data stream for an out-of-band message,
  3543.                               and to 0 if it does not point to a mark.
  3544.  
  3545.  
  3546.  
  3547.    RETURN VALUES
  3548.          IoctlSocket() returns 0 on success for most requests.   Some
  3549.          specialized requests may return non-zero values on success; On
  3550.          failure,  IoctlSocket()returns -1 and sets errno to indicate
  3551.          the error.
  3552.  
  3553.  
  3554.    ERRORS
  3555.          EBADF          fd is not a valid descriptor.
  3556.  
  3557.  
  3558.          EINVAL         request or arg is not valid.
  3559.  
  3560.  
  3561.          IoctlSocket() will also fail if the object on which the function
  3562.          is  being performed detects an error. In this case, an error
  3563.          code specific  to  the  object  and  the  function  will  be
  3564.          returned.
  3565.  
  3566.  
  3567.    SEE ALSO
  3568.          getsockopt(), SetSocketSignals(), setsockopt()
  3569.  
  3570.  
  3571. 144    Section B.1                 AmiTCP/IP                  System Manual
  3572.  
  3573.  
  3574.  
  3575. B.1.10   listen()
  3576.  
  3577.    NAME
  3578.          listen -listen for connections on a socket
  3579.  
  3580.  
  3581.    SYNOPSIS
  3582.          success =listen(s, backlog)
  3583.          D0                D0 D1
  3584.  
  3585.  
  3586.          long listen(long, long);
  3587.  
  3588.  
  3589.    FUNCTION
  3590.          To accept  connections,  a  socket  is  first  created  with
  3591.          socket(), a  backlog for incoming connections is specified
  3592.          with listen() and then the  connections  are  accepted  with
  3593.          accept().   The  listen()  call  applies only to socket of
  3594.          type SOCK_STREAM.
  3595.  
  3596.  
  3597.          The backlog parameter defines the maximum length  the  queue
  3598.          of pending connections may grow to.  If a connection request
  3599.          arrives with the queue full the client will receive an error
  3600.          with an indication of ECONNREFUSED.
  3601.  
  3602.  
  3603.    RETURN VALUES
  3604.           0    on success.
  3605.  
  3606.  
  3607.          -1    on failure and sets errno to indicate the error.
  3608.  
  3609.  
  3610.    ERRORS
  3611.          EBADF              -s is not a valid descriptor.
  3612.  
  3613.  
  3614.          EOPNOTSUPP         - The socketis not of a  type  that  sup-
  3615.                               ports listen().
  3616.  
  3617.  
  3618.    SEE ALSO
  3619.          accept(),connect(), socket()
  3620.  
  3621.  
  3622.    BUGS
  3623.          The backlog is currently limited (silently) to 5.
  3624.  
  3625.  
  3626. System Manual                  AmiTCP/IP                 Section B.1    145
  3627.  
  3628.  
  3629.  
  3630. B.1.11   recv()
  3631.  
  3632.  
  3633.  
  3634.    NAME
  3635.          recv, recvfrom, - receive a message from a socket
  3636.  
  3637.  
  3638.    SYNOPSIS
  3639.          #include <sys/types.h>
  3640.          #include <sys/socket.h>
  3641.  
  3642.  
  3643.         nbytes = recv(s, buf, len, flags)
  3644.         D0            D0 A0    D1    D2
  3645.  
  3646.  
  3647.         long recv(long, char *, long, long);
  3648.  
  3649.  
  3650.         nbytes = recvfrom(s, buf, len, flags, from, fromlen)
  3651.         D0                 D0 A0    D1    D2     A1    A2
  3652.  
  3653.  
  3654.         long recvfrom(long, char *, long, long,
  3655.                           struct sockaddr *, long *);
  3656.  
  3657.  
  3658.    FUNCTION
  3659.          s is a socket created with socket().  recv() and recvfrom(),
  3660.          are used to  receive messages from  another socket.  recv()
  3661.          may  be used  only on a connected socket  (see  connect()),
  3662.          while  recvfrom() may be used  to receive data  on a  socket
  3663.          whether it is in a connected state or not.
  3664.  
  3665.  
  3666.          If from is not a NULL pointer, the  source  address  of  the
  3667.          message is filled in.  fromlen is a value-result parameter,
  3668.          initialized to the size of the buffer associated with  from,
  3669.          and  modified  on  return to indicate the actual size of the
  3670.          address stored  there.    The  length  of  the  message    is
  3671.          returned.   If  a message is too long to fitin the supplied
  3672.          buffer, excess bytes may be discarded depending on the  type
  3673.          of socketthe message is received from (see socket()).
  3674.  
  3675.  
  3676.          If no messages are available at the socket, the receive call
  3677.          waits  for  a  message  to arrive, unless the socket is non-
  3678.          blocking(see IoctlSocket()) in which case -1  is  returned
  3679.          with theexternal variable errno set to EWOULDBLOCK.
  3680.  
  3681.  
  3682.          The select() call may be used to determine when  more  data
  3683.          arrive.
  3684.  
  3685.  
  3686.          The flagsparameter is formed by ORing one or  more  of  the
  3687.          following:
  3688.  
  3689.  
  3690.          MSG_OOB      - Read any "out-of-band" data  present on  the
  3691.                          socket,  rather  than  the  regular "in-band"
  3692.                          data.
  3693.  
  3694.  
  3695. 146    Section B.1                 AmiTCP/IP                  System Manual
  3696.  
  3697.  
  3698.  
  3699.          MSG_PEEK     - "Peek" at the data present onthe socket; the
  3700.                          data  are returned, but not consumed, so that
  3701.                          a subsequent receive operation will  see  the
  3702.                          same data.
  3703.  
  3704.  
  3705.    RETURN VALUES
  3706.          These calls return the number of bytes received, or -1 if an
  3707.          error occurred.
  3708.  
  3709.  
  3710.    ERRORS
  3711.          EBADF              -s is an invalid descriptor.
  3712.  
  3713.  
  3714.          EINTR              -The operation was interrupted by a break
  3715.                               signal.
  3716.  
  3717.  
  3718.          EWOULDBLOCK        - The socket is  marked  non-blocking and
  3719.                               the requested operation would block.
  3720.  
  3721.  
  3722.    SEE ALSO
  3723.          connect(), getsockopt(), IoctlSocket(), select(), send(),
  3724.          SetSocketSignals(), socket()
  3725.  
  3726.  
  3727. System Manual                  AmiTCP/IP                 Section B.1    147
  3728.  
  3729.  
  3730.  
  3731. B.1.12   select()
  3732.  
  3733.  
  3734.  
  3735.    NAME
  3736.          select --synchronous I/O multiplexing (stub/inline function)
  3737.          WaitSelect -- select() with Amiga Wait() function.
  3738.  
  3739.  
  3740.    SYNOPSIS
  3741.          #include <sys/types.h>
  3742.          #include <sys/time.h>
  3743.  
  3744.  
  3745.          n = select (nfds, readfds, writefds, exceptfds, timeout)
  3746.  
  3747.  
  3748.          long select(long, fd_set *, fd_set *, fd_set *,
  3749.                       struct timeval *);
  3750.  
  3751.  
  3752.          n = WaitSelect (nfds, readfds, writefds, exceptfds, timeout,
  3753.          D0               D0    A0        A1         A2         A3
  3754.                       sigmp)
  3755.                       D1
  3756.  
  3757.  
  3758.          long WaitSelect(long, fd_set *, fd_set *, fd_set *,
  3759.                           struct timeval *, long *);
  3760.  
  3761.  
  3762.          FD_SET (fd, &fdset)
  3763.          FD_CLR (fd, &fdset)
  3764.          FD_ISSET(fd, &fdset)
  3765.          FD_ZERO (&fdset)
  3766.          long fd;
  3767.          fd_set fdset;
  3768.  
  3769.  
  3770.    DESCRIPTION
  3771.          select()examines the socket descriptor sets whose addresses
  3772.          are passed in readfds,  writefds,  and exceptfds  to  see if
  3773.          some of their descriptors are ready  for reading,  ready for
  3774.          writing, or have an exceptional condition pending.  nfds is
  3775.          the  number  of bits to be checked in  each  bit  mask  that
  3776.          representa file descriptor; the descriptors from 0  through
  3777.          (nfds - 1) in the descriptor sets  are examined.  On return,
  3778.          select() replaces  the  given descriptor sets  with subsets
  3779.          consisting of  those descriptors  that  are  ready  for  the
  3780.          requestedoperation.  The total number  of ready descriptors
  3781.          in all the sets is returned.
  3782.  
  3783.  
  3784.          WaitSelect() also takes a signal mask which is waited during
  3785.          normal select() operation. If one of these singals is recei-
  3786.          ved,  WaitSelect() returns  and has  re-set  the signal mask
  3787.          to returnthose signals that  have arrived.  Normal select()
  3788.          return values are returned.
  3789.  
  3790.  
  3791.          The descriptor sets are stored as bit fields  in  arrays  of
  3792.          integers.   The following macros are provided for manipulat-
  3793.  
  3794.  
  3795. 148    Section B.1                 AmiTCP/IP                  System Manual
  3796.  
  3797.  
  3798.  
  3799.          ing suchdescriptor sets:  FD_ZERO  (&fdset)  initializes  a
  3800.          descriptor  set  fdset to the null set.  FD_SET(fd, &fdset )
  3801.          includesa particular descriptor fd  in  fdset.   FD_CLR(fd,
  3802.          &fdset) removes  fd  from  fdset.   FD_ISSET(fd, &fdset) is
  3803.          nonzero if fd is a member of  fdset,  zero  otherwise.   The
  3804.          behavior  of these macros is undefined if a descriptor value
  3805.          is less than zero or greater than or  equal  to  FD_SETSIZE,
  3806.          which  is normally  at least equal to the maximum number of
  3807.          descriptors supported by the system.
  3808.  
  3809.  
  3810.          If timeout is not a NULL pointer,  it  specifies  a  maximum
  3811.          interval to wait for the selection to complete.  If timeout
  3812.          is a NULL pointer,  the  select  blocks  indefinitely.   To
  3813.          effect  a poll,  the  timeout argument should be a non-NULL
  3814.          pointer,pointing to a zero-valued timeval structure.
  3815.  
  3816.  
  3817.          Any of readfds, writefds, and exceptfds may be given as NULL
  3818.          pointersif no descriptors are of interest.
  3819.  
  3820.  
  3821.          Selectingtrue for reading on a socket descriptor upon which
  3822.          a  listen() call has been performed indicates that a subse-
  3823.          quent accept() call on that descriptor will not block.
  3824.  
  3825.  
  3826.    RETURN VALUES
  3827.          select()returns a non-negative value on success. A positive
  3828.          value indicates the number of ready descriptors in the
  3829.          descriptor sets. 0 indicates that the time limit referred to
  3830.          by timeout expired or that the operation was interrupted
  3831.          either bya break signal or by arrival of a signal specified
  3832.          in *sigmp. On failure, select() returns -1, sets errno to
  3833.          indicatethe error, and the descriptor sets are not changed.
  3834.  
  3835.  
  3836.    ERRORS
  3837.          EBADF         - One  of  the  descriptor  sets  specified  an
  3838.                          invalid descriptor.
  3839.  
  3840.  
  3841.          EINTR         - one of the signals in SIGINTR  mask (see Set-
  3842.                          SocketSignals())   is  set  and  it  was not
  3843.                          requested in WaitSelect() call.
  3844.  
  3845.  
  3846.          EINVAL       - A component of the pointed-to time  limit is
  3847.                          outside  the  acceptable range: t_sec must be
  3848.                          between 0 and 10^8,inclusive. t_usec must be
  3849.                          greater  than  or equal  to 0, and less than
  3850.                          10^6.
  3851.  
  3852.  
  3853.    SEE ALSO
  3854.          accept(),  connect(), getdtablesize(), listen(), recv(),
  3855.          send(), SetDTableSize(), SetSocketSignals()
  3856.  
  3857.  
  3858.    NOTES
  3859.  
  3860.  
  3861. System Manual                  AmiTCP/IP                 Section B.1    149
  3862.  
  3863.  
  3864.  
  3865.          Under rare  circumstances,  select()  may  indicate  that  a
  3866.          descriptor  is  ready for writing when in fact an attempt to
  3867.          write would block.  This  can  happen  if  system  resources
  3868.          necessary for  a  write are exhausted or otherwise unavail-
  3869.          able.  Ifan application deems it critical that writes to  a
  3870.          file  descriptor not block, it should set the descriptor for
  3871.          non-blocking I/O using the FIOASYNC request to IoctlSocket().
  3872.  
  3873.  
  3874.          Default   system    limit  for  open  socket  descriptors  is
  3875.          currently 64. However,  in  order  to accommodate  programs
  3876.          which might  potentially  use  a larger number of open files
  3877.          with select, it is possible  to  increase this size within a
  3878.          program by  providing  a  larger definition  of  FD_SETSIZE
  3879.          before    the    inclusion     of    <sys/types.h>     and    use
  3880.          SetDTableSize(FD_SETSIZE) call directly after OpenLibrary().
  3881.  
  3882.  
  3883.    BUGS
  3884.          select()should probably return the time remaining from  the
  3885.          original timeout,  if  any,  by modifying the time value in
  3886.          place.  This may be implemented in future  versions  of  the
  3887.          system.   Thus,  it  is  unwise  to  assume that the timeout
  3888.          pointer will be unmodified by the select() call.
  3889.  
  3890.  
  3891. 150    Section B.1                 AmiTCP/IP                  System Manual
  3892.  
  3893.  
  3894.  
  3895. B.1.13   send()
  3896.  
  3897.  
  3898.  
  3899.    NAME
  3900.          send, sendto - send a message from a socket
  3901.  
  3902.  
  3903.    SYNOPSIS
  3904.          #include <sys/types.h>
  3905.          #include <sys/socket.h>
  3906.  
  3907.  
  3908.          nbytes =send(s, msg, len, flags)
  3909.          D0            D0  A0   D1    D2
  3910.  
  3911.  
  3912.          int send(int, char *, int, int);
  3913.  
  3914.  
  3915.          nbytes =sendto(s, msg, len, flags, to, tolen)
  3916.          D0               D0 A0    D1    D2     A1  D3
  3917.  
  3918.  
  3919.          int send(int, char *, int, int, struct sockaddr *, int);
  3920.  
  3921.  
  3922.    FUNCTION
  3923.          s is a socket created with socket().  send() and sendto() are
  3924.          used  totransmit a message to  another socket. send() may be
  3925.          used  only when the socket  is  in a connected  state,  while
  3926.          sendto()may be used at any time.
  3927.  
  3928.  
  3929.          The address of the target  is given by to with tolen specify-
  3930.          ing its size.  The length of the message is given by len.  If
  3931.          the  message is  too  long  to  pass  atomically  through the
  3932.          underlying protocol, then the error EMSGSIZE is returned, and
  3933.          the message is not transmitted.
  3934.  
  3935.  
  3936.          No indication of failure to deliver is implicit  in a send().
  3937.          Return values of -1 indicate some locally detected errors.
  3938.  
  3939.  
  3940.          If no buffer space is  available at the socket  to  hold  the
  3941.          message to  be  transmitted,  then  send() normally  blocks,
  3942.          unless the  socket  has been placed in non-blocking I/O mode.
  3943.          The select() call may be used to determine when  it  is  pos-
  3944.          sible tosend more data.
  3945.  
  3946.  
  3947.          The flagsparameter is formed  by ORing  one  or  more of the
  3948.          following:
  3949.  
  3950.  
  3951.          MSG_OOB           - Send  ``out-of-band''  data  on  sockets
  3952.                               that  support this notion.  The underly-
  3953.                               ing protocol must  also  support  ``out-
  3954.                               of-band''    data.      Currently,   only
  3955.                               SOCK_STREAM  sockets  created    in    the
  3956.                               AF_INET  address  family support out-of-
  3957.                               band data.
  3958.  
  3959.  
  3960. System Manual                  AmiTCP/IP                 Section B.1    151
  3961.  
  3962.  
  3963.  
  3964.          MSG_DONTROUTE      - The SO_DONTROUTE optionis turned on for
  3965.                               the  duration of the operation.  This is
  3966.                               usually used only by diagnostic or rout-
  3967.                               ing programs.
  3968.  
  3969.  
  3970.    RETURN VALUES
  3971.          On success, these functions return the number of bytes sent.
  3972.          On  failure,  they  return  -1 and set errno to indicate the
  3973.          error.
  3974.  
  3975.  
  3976.    ERRORS
  3977.          EBADF              -s is an invalid descriptor.
  3978.  
  3979.  
  3980.          EINTR              -The operation was interrupted by a break
  3981.                                signal.
  3982.  
  3983.  
  3984.          EINVAL            - len is not the size of a  valid  address
  3985.                               for the specified address family.
  3986.  
  3987.  
  3988.          EMSGSIZE          - The socket requires that message be sent
  3989.                               atomically,  and the size of the message
  3990.                               to be sent made this impossible.
  3991.  
  3992.  
  3993.          ENOBUFS           - The system was  unable  to  allocate  an
  3994.                               internal    buffer.    The  operation  may
  3995.                               succeed when buffers become available.
  3996.  
  3997.  
  3998.          ENOBUFS           - The output queue for a network interface
  3999.                               was full.  This generally indicates that
  4000.                               the interface has stopped  sending,  but
  4001.                               may be caused by transient congestion.
  4002.  
  4003.  
  4004.          EWOULDBLOCK        - The socket is  marked  non-blocking and
  4005.                               the requested operation would block.
  4006.  
  4007.  
  4008.    SEE ALSO
  4009.          connect(), getsockopt(), recv(), select(), socket()
  4010.  
  4011.  
  4012. 152    Section B.1                 AmiTCP/IP                  System Manual
  4013.  
  4014.  
  4015.  
  4016. B.1.14   shutdown()
  4017.  
  4018.    NAME
  4019.          shutdown- shut down part of a full-duplex connection
  4020.  
  4021.  
  4022.    SYNOPSIS
  4023.          success =shutdown(s, how)
  4024.          D0                  D0 D1
  4025.  
  4026.  
  4027.          long shutdown(long, long);
  4028.  
  4029.  
  4030.    DESCRIPTION
  4031.          The shutdown() call causes all or part of a full-duplex con-
  4032.          nection on the socket associated with s to be shut down.  If
  4033.          how is 0,then further receives will be disallowed.  If  how
  4034.          is  1,  then further sends will be disallowed.  If how is 2,
  4035.          then further sends and receives will be disallowed.
  4036.  
  4037.  
  4038.    RETURN VALUES
  4039.           0 - on success.
  4040.  
  4041.  
  4042.          -1 - on failure and sets errno to indicate the error.
  4043.  
  4044.  
  4045.    ERRORS
  4046.          EBADF         - s is not a valid descriptor.
  4047.  
  4048.  
  4049.          ENOTCONN     - The specified socket is not connected.
  4050.  
  4051.  
  4052.    SEE ALSO
  4053.          connect(),  socket()
  4054.  
  4055.  
  4056.    BUGS
  4057.          The how values should be defined constants.
  4058.  
  4059.  
  4060. System Manual                  AmiTCP/IP                 Section B.1    153
  4061.  
  4062.  
  4063.  
  4064. B.1.15   socket()
  4065.  
  4066.  
  4067.  
  4068.    NAME
  4069.          socket -create an endpoint for communication
  4070.  
  4071.  
  4072.    SYNOPSIS
  4073.          #include <sys/types.h>
  4074.          #include <sys/socket.h>
  4075.  
  4076.  
  4077.          s = socket(domain, type, protocol)
  4078.          D0          D0      D1    D2
  4079.  
  4080.  
  4081.          long socket(long, long, long);
  4082.  
  4083.  
  4084.    FUNCTION
  4085.          socket()creates an endpoint for communication and returns a
  4086.          descriptor.
  4087.  
  4088.  
  4089.          The  domain  parameter  specifies  a  communications  domain
  4090.          within which communication will take place; this selects the
  4091.          protocol family  which should be used.  The protocol family
  4092.          generally is  the  same  as  the  address  family  for  the
  4093.          addressessupplied in later operations on the socket.  These
  4094.          families are defined  in the  include file  <sys/socket.h>.
  4095.          The currently understood formats are
  4096.  
  4097.  
  4098.                  PF_INET - (ARPA Internet protocols)
  4099.  
  4100.  
  4101.          The socket has the indicated type, which specifies the
  4102.          semanticsof communication.  Currently defined types are:
  4103.  
  4104.  
  4105.                  SOCK_STREAM
  4106.                  SOCK_DGRAM
  4107.                  SOCK_RAW
  4108.  
  4109.  
  4110.          A  SOCK_STREAM type  provides  sequenced,  reliable, two-way
  4111.          connection  based    byte  streams.    An    out-of-band  data
  4112.          transmission  mechanism  may  be  supported.   A  SOCK_DGRAM
  4113.          socket supports datagrams  (connectionless, unreliable  mes-
  4114.          sages  of a  fixed    (typically    small)  maximum  length).
  4115.          SOCK_RAW   sockets    provide  access  to   internal  network
  4116.          interfaces.
  4117.  
  4118.  
  4119.          The protocol specifies a particular protocol to be used with
  4120.          the socket.  Normally  only a single protocol exists to sup-
  4121.          port a particular socket type  within a given  protocol fam-
  4122.          ily.  However, it is possible that many protocols may exist,
  4123.          in whichcase a  particular protocol  must be  specified  in
  4124.          this manner.  The  protocol number to use  is  particular to
  4125.          the "communication domain" in which communication is to take
  4126.          place.
  4127.  
  4128.  
  4129. 154    Section B.1                 AmiTCP/IP                  System Manual
  4130.  
  4131.  
  4132.  
  4133.          Sockets of type SOCK_STREAM  are  full-duplex byte  streams,
  4134.          similar to pipes.    A  streamsocket must be in  a connected
  4135.          state before any data may be sent or received on it.  A con-
  4136.          nection  to another socket is created with a connect() call.
  4137.          Once  connected, data  may be  transferred using send()  and
  4138.          recv()  or their variant calls.   When  a  session  has been
  4139.          completeda CloseSocket()  may  be  performed.   Out-of-band
  4140.          data mayalso  be transmitted  as  described  in  send() and
  4141.          receivedas described in recv().
  4142.  
  4143.  
  4144.          The communications protocols used to implement a SOCK_STREAM
  4145.          insure that data is  not lost or  duplicated.  If a piece of
  4146.          data for  which the peer protocol has buffer space cannot be
  4147.          successfully transmitted within a reasonable length of time,
  4148.          then the connection  is  considered broken  and  calls will
  4149.          indicatean error with -1 returns and with ETIMEDOUT  as the
  4150.          specificerror code (see Errno()).  The protocols optionally
  4151.          keep sockets "warm" by  forcing transmissions roughly  every
  4152.          minute inthe absence of other activity.
  4153.  
  4154.  
  4155.          SOCK_DGRAM  and SOCK_RAW sockets allow sending of  datagrams
  4156.          to  correspondents  named in send()  calls.   Datagrams  are
  4157.          generally received  with  recv(), which  returns  the  next
  4158.          datagramwith its return address.
  4159.  
  4160.  
  4161.          The operation of  sockets  is  controlled  by  socket  level
  4162.          options.   These  options  aredefined in the file socket.h.
  4163.          getsockopt()  and  setsockopt()  are  used  to  get  and  set
  4164.          options, respectively.
  4165.  
  4166.  
  4167.    RETURN VALUES
  4168.          socket()returns a non-negative descriptor on  success.   On
  4169.          failure,it returns -1 and sets errno to indicate the error.
  4170.  
  4171.  
  4172.    ERRORS
  4173.          EACCES          - Permission to create  a  socket  of  the
  4174.                             specified    type    and/or    protocol   is
  4175.                             denied.
  4176.  
  4177.  
  4178.          EMFILE          - The per-process descriptor table is
  4179.                             full.
  4180.  
  4181.  
  4182.          ENOBUFS         - Insufficient buffer space is available.
  4183.                             The socket cannot be created until suf-
  4184.                             ficient resources are freed.
  4185.  
  4186.  
  4187.          EPROTONOSUPPORT - The protocol type or the specified  pro-
  4188.                             tocol is not supported within this
  4189.                             domain.
  4190.  
  4191.  
  4192. System Manual                  AmiTCP/IP                 Section B.1    155
  4193.  
  4194.  
  4195.  
  4196.          EPROTOTYPE       - The protocol is the wrong type for the
  4197.                             socket.
  4198.  
  4199.  
  4200.    SEE ALSO
  4201.          accept(),bind(), CloseSocket(), connect(), getsockname(),
  4202.          getsockopt(), IoctlSocket(), listen(), recv(), select(),
  4203.          send(), shutdown(), WaitSelect()
  4204.  
  4205.  
  4206. 156    Section B.2                 AmiTCP/IP                  System Manual
  4207.  
  4208.  
  4209.  
  4210. B.2     Other  BSD  Functions  Related  to  Sockets
  4211.  
  4212.  
  4213. B.2.1   getdtablesize()
  4214.  
  4215.    NAME
  4216.          getdtablesize - get socket descriptor table size
  4217.  
  4218.  
  4219.    SYNOPSIS
  4220.  
  4221.  
  4222.          nfds = getdtablesize()
  4223.          D0
  4224.  
  4225.  
  4226.          long getdtablesize(void);
  4227.  
  4228.  
  4229.    FUNCTION
  4230.          Return value of maximum  number of open socket  descriptors.
  4231.          Larger  socket  descriptor  table  can   be  allocated  with
  4232.          SetDTableSize() call.
  4233.  
  4234.  
  4235.    SEE ALSO
  4236.          SetDTableSize()
  4237.  
  4238.  
  4239. System Manual                  AmiTCP/IP                 Section B.2    157
  4240.  
  4241.  
  4242.  
  4243. B.2.2   Syslog()
  4244.  
  4245.  
  4246.  
  4247.    NAME
  4248.          syslog -write message to AmiTCP/IP log.
  4249.  
  4250.  
  4251.    SYNOPSIS
  4252.          #include <syslog.h>
  4253.  
  4254.  
  4255.          void syslog(unsigned long level, char * format, ...);
  4256.  
  4257.  
  4258.          Syslog(level, format, ap)
  4259.                D0     A0       A1
  4260.  
  4261.  
  4262.          VOID Syslog(unsigned long, const char *, va_list);
  4263.  
  4264.  
  4265.  
  4266.    FUNCTION
  4267.          Writes the message given as format string and arguments
  4268.          (printf-style) both to the log file and to the console,
  4269.          execpt ifthe level is LOG_EMERG, which is used by panic(),
  4270.          in whichcase only the log file is used since panic()
  4271.          generatesa User Request.
  4272.  
  4273.  
  4274.          The levelis selected from an ordered list:
  4275.  
  4276.  
  4277.               LOG_EMERG            A panic condition.
  4278.  
  4279.  
  4280.               LOG_ALERT            A condition thatshould be
  4281.                                    corrected immediately, such as a
  4282.                                    corrupted system database.
  4283.  
  4284.  
  4285.               LOG_CRIT             Critical conditions, such  as  hard
  4286.                                    device errors.
  4287.  
  4288.  
  4289.               LOG_ERR              Errors.
  4290.  
  4291.  
  4292.               LOG_WARNING          Warning messages.
  4293.  
  4294.  
  4295.               LOG_NOTICE           Conditions that are not error  con-
  4296.                                    ditions,   butthat may require spe-
  4297.                                    cial  handling.
  4298.  
  4299.  
  4300.               LOG_INFO             Informational messages.
  4301.  
  4302.  
  4303.               LOG_DEBUG            Messages that  contain information
  4304.                                    normally of use only when debugging
  4305.                                    a  program.
  4306.  
  4307.  
  4308.    INPUTS
  4309.          Level     - indicates the type of the message. The levels
  4310.                       are defined in sys/syslog.h and listed above.
  4311.  
  4312.  
  4313. 158    Section B.2                 AmiTCP/IP                  System Manual
  4314.  
  4315.  
  4316.  
  4317.          format    - This is a printf-style format string as defined
  4318.                       in  exec.library/RawDoFmt().
  4319.  
  4320.  
  4321.          arguments- as in printf().
  4322.  
  4323.  
  4324.          ap         - pointer to an array of arguments.
  4325.  
  4326.  
  4327.    RESULT
  4328.          Returns no value.
  4329.  
  4330.  
  4331.    EXAMPLES
  4332.          To  log a message at priority  LOG_INFO, it would  make the
  4333.          followingcall to syslog:
  4334.  
  4335.  
  4336.               syslog(LOG_INFO,  "Connection from host %s",
  4337.                       CallingHost);
  4338.  
  4339.  
  4340.    NOTES
  4341.          As Exec RawDoFmt() used to do formatting expects by default
  4342.          short (16bit long) integers you should use the `l'-modifier
  4343.          when appopriate. See your compiler documentation about how
  4344.          it passesarguments on a vararg list.
  4345.  
  4346.  
  4347.          This function is callable from interrupts.
  4348.  
  4349.  
  4350.    BUGS
  4351.          Because there is a limited number of internal messages used
  4352.          by the logging system, some log messages may get lost if a
  4353.          high priority task or interrupt handler sends many messages
  4354.          in succession. If this happens, the next log message tells
  4355.          the fact.
  4356.  
  4357.  
  4358.    SEE ALSO
  4359.         exec.library/RawDoFmt()
  4360.  
  4361.  
  4362. System Manual                  AmiTCP/IP                 Section B.3    159
  4363.  
  4364.  
  4365.  
  4366. B.3     Network  Data  and Address  Manipulation
  4367.  
  4368.  
  4369.  
  4370. B.3.1   inet _addr()
  4371.  
  4372.  
  4373.    NAME
  4374.          inet_addr,  inet_network,  Inet_MakeAddr,  Inet_LnaOf,
  4375.          Inet_NetOf, Inet_NtoA - Internet address manipulation
  4376.  
  4377.  
  4378.          inet_makeaddr, inet_lnaof, inet_netof,
  4379.          inet_ntoa-- inline/stub functions to handle structure arguments
  4380.  
  4381.  
  4382.    SYNOPSIS
  4383.          #include <netinet/in.h>
  4384.  
  4385.  
  4386.          addr = inet_addr(cp)
  4387.          D0                A0
  4388.  
  4389.  
  4390.          unsignedlong inet_addr(char *);
  4391.  
  4392.  
  4393.          net = inet_network(cp)
  4394.          D0                  A0
  4395.  
  4396.  
  4397.          unsignedlong inet_network(char *);
  4398.  
  4399.  
  4400.          in_addr =Inet_MakeAddr(net, lna)
  4401.          D0                       D0   D1
  4402.  
  4403.  
  4404.          unsignedlong Inet_MakeAddr(long, long);
  4405.  
  4406.  
  4407.          lna = Inet_LnaOf(in)
  4408.          D0                D0
  4409.  
  4410.  
  4411.          long Inet_LnaOf(unsigned long);
  4412.  
  4413.  
  4414.          net = Inet_NetOf(in)
  4415.          D0                D0
  4416.  
  4417.  
  4418.          long Inet_NetOf(unsigned long);
  4419.  
  4420.  
  4421.          addr = Inet_NtoA(in)
  4422.          DO                D0
  4423.  
  4424.  
  4425.          char * Inet_NtoA(unsigned long);
  4426.  
  4427.  
  4428.  
  4429.          in_addr =inet_makeaddr(net, lna)
  4430.  
  4431.  
  4432.          struct in_addr inet_makeaddr(long, long);
  4433.  
  4434.  
  4435.          lna = inet_lnaof(in)
  4436.  
  4437.  
  4438. 160    Section B.3                 AmiTCP/IP                  System Manual
  4439.  
  4440.  
  4441.  
  4442.          int inet_lnaof(struct in_addr);
  4443.  
  4444.  
  4445.          net = inet_netof(in)
  4446.  
  4447.  
  4448.          int inet_netof(struct in_addr);
  4449.  
  4450.  
  4451.          addr = inet_ntoa(in)
  4452.  
  4453.  
  4454.          char * inet_ntoa(struct in_addr);
  4455.  
  4456.  
  4457.    IMPLEMENTATION NOTE
  4458.          Return  value  of  Inet_MakeAddr()  and  argument  types  of
  4459.          Inet_LnaOf(), Inet_NetOf() and Inet_NtoA() are longs instead
  4460.          of  struct  in_addr.  The original behaviour  is achieved by
  4461.          using  included  stub  functions (lower case function names)
  4462.          which handle structure arguments.
  4463.  
  4464.  
  4465.    DESCRIPTION
  4466.          The routines inet_addr() and inet_network()  each  interpret
  4467.          character strings  representing  numbers  expressed  in the
  4468.          Internetstandard `.' notation, returning  numbers  suitable
  4469.          for  use as Internet addresses and Internet network numbers,
  4470.          respectively.   The routine inet_makeaddr() takes an Internet
  4471.          network number and a local network address and constructs an
  4472.          Internetaddress from it.    The  routines  inet_netof()  and
  4473.          inet_lnaof()   break apart Internet host addresses, returning
  4474.          the network number and local network address  part,  respec-
  4475.          tively.
  4476.  
  4477.  
  4478.          The routine inet_ntoa() returns a pointer to a string in the
  4479.          base 256notation ``d.d.d.d'' described below.
  4480.  
  4481.  
  4482.          All Internet address are returned in  network  order  (bytes
  4483.          ordered from left to right).  All network numbers and local
  4484.          address parts are returned as machine format integer values.
  4485.  
  4486.  
  4487.    INTERNET ADDRESSES
  4488.          Values specified using the `.'  notation  take  one  of  the
  4489.  
  4490.  
  4491.          following forms:
  4492.  
  4493.  
  4494.               a.b.c.d
  4495.               a.b.c
  4496.               a.b
  4497.               a
  4498.  
  4499.  
  4500.          When fourparts are specified, each is interpreted as a byte
  4501.          of data and assigned, from left to right, to  the four bytes
  4502.          of  an Internet address.  Note: when  an Internet address is
  4503.          viewed  as  a  32-bit  integer  quantity  on  little  endian
  4504.          systems, the  bytes referred to  above appear  as  d.c.b.a.
  4505.  
  4506.  
  4507. System Manual                  AmiTCP/IP                 Section B.3    161
  4508.  
  4509.  
  4510.  
  4511.          bytes areordered from right to left.
  4512.  
  4513.  
  4514.          When a three part address is specified,  the  last  part  is
  4515.          interpreted  as  a  16-bit  quantity and placed in the right
  4516.          most twobytes of the network address.  This makes the three
  4517.          part  address  format convenient for specifying Class B net-
  4518.          work addresses as "128.net.host".
  4519.  
  4520.  
  4521.          When a two part address is supplied, the last part is inter-
  4522.          preted  as  a  24-bit  quantity and placed in the right most
  4523.          three bytes of the network address.  This makes the two part
  4524.          address format  convenient  for  specifying Class A network
  4525.          addressesas "net.host".
  4526.  
  4527.  
  4528.          When onlyone part is given, the value is stored directly in
  4529.          the network address without any byte rearrangement.
  4530.  
  4531.  
  4532.          All numbers supplied as ``parts'' in a `.' notation  may  be
  4533.          decimal, octal,  or  hexadecimal,  as  specified  in  the C
  4534.          language(that is, a leading 0x or 0X  implies  hexadecimal;
  4535.          otherwise,   a leading0 implies octal; otherwise, the number
  4536.          is interpreted as decimal).
  4537.  
  4538.  
  4539.    RETURN VALUE
  4540.          The value-1 is returned by inet_addr()  and  inet_network()
  4541.          for malformed requests.
  4542.  
  4543.  
  4544.    BUGS
  4545.          The problem of host byte ordering versus network byte order-
  4546.          ing  is confusing.  A simple way to specify Class C network
  4547.          addressesin a manner similar to that for Class B and  Class
  4548.          A is needed.
  4549.  
  4550.  
  4551.          The return value from inet_ntoa() points to static buffer
  4552.          which  is overwritten  in  each inet_ntoa() call.
  4553.  
  4554.  
  4555. 162    Section B.4                 AmiTCP/IP                  System Manual
  4556.  
  4557.  
  4558.  
  4559. B.4     Network,  Protocol and  Service  Queries
  4560.  
  4561.  
  4562.  
  4563. B.4.1   gethostbyname()
  4564.  
  4565.  
  4566.    NAME
  4567.          gethostbyname, gethostbyaddr  - get network host entry
  4568.  
  4569.  
  4570.    SYNOPSIS
  4571.          #include <sys/types.h>
  4572.          #include <sys/socket.h>
  4573.          #include <netdb.h>
  4574.  
  4575.  
  4576.          hostent =gethostbyname(name)
  4577.          D0                       A0
  4578.  
  4579.  
  4580.          struct hostent *gethostbyname(char *);
  4581.  
  4582.  
  4583.          hostent =gethostbyaddr(addr, len, type)
  4584.          D0                       A0     D0   D1
  4585.  
  4586.  
  4587.          struct hostent *gethostbyaddr(caddr_t, LONG, LONG);
  4588.  
  4589.  
  4590.  
  4591.    DESCRIPTION
  4592.          gethostbyname() and gethostbyaddr() both return a pointer
  4593.          to an object with the following structure containing the
  4594.          data received from a name server or the broken-out fields
  4595.          of a linein netdb configuration file.  In the case of
  4596.          gethostbyaddr(), addr is a pointer to the binary format
  4597.          address of length len (not a character string) and type is
  4598.          an address family as defined in <sys/socket.h>.
  4599.  
  4600.  
  4601.            struct hostent -
  4602.              char *h_name;       /* official name of host */
  4603.              char **h_aliases;   /* alias list */
  4604.              int  h_addrtype;    /* address type */
  4605.              int  h_length;      /* length of address */
  4606.              char **h_addr_list; /* list of addresses from name server */
  4607.            ";
  4608.  
  4609.  
  4610.          The members of this structure are:
  4611.  
  4612.  
  4613.          h_name               Official name of the host.
  4614.  
  4615.  
  4616.          h_aliases           A zero  terminated  array  of  alternate
  4617.                               names for the host.
  4618.  
  4619.  
  4620.          h_addrtype           The  type  of  address  being  returned;
  4621.                               currently always AF_INET.
  4622.  
  4623.  
  4624.          h_length            The length, in bytes, of the address.
  4625.  
  4626.  
  4627. System Manual                  AmiTCP/IP                 Section B.4    163
  4628.  
  4629.  
  4630.  
  4631.          h_addr_list          A pointer to a list of network addresses
  4632.                               for  the named host.  Host addresses are
  4633.                               returned in network byte order.
  4634.  
  4635.  
  4636.    DIAGNOSTICS
  4637.          A NULL pointer is returned if no matching entry was found or
  4638.          error occured.
  4639.  
  4640.  
  4641.    BUGS
  4642.          All information is contained in a static area so it must  be
  4643.          copied ifit is to be saved.  Only the Internet address for-
  4644.          mat is currently understood.
  4645.  
  4646.  
  4647.    SEE ALSO
  4648.          AmiTCP/IP configuration
  4649.  
  4650.  
  4651. 164    Section B.4                 AmiTCP/IP                  System Manual
  4652.  
  4653.  
  4654.  
  4655. B.4.2   getnetbyname()
  4656.  
  4657.  
  4658.  
  4659.    NAME
  4660.          getnetbyname, getnetbyaddr - get network entry
  4661.  
  4662.  
  4663.    SYNOPSIS
  4664.          #include <netdb.h>
  4665.  
  4666.  
  4667.          netent =getnetbyname(name)
  4668.          D0                     A0
  4669.  
  4670.  
  4671.          struct netent *getnetbyname(char *);
  4672.  
  4673.  
  4674.          netent =getnetbyaddr(net, type)
  4675.          D0                     D0   D1
  4676.  
  4677.  
  4678.          struct netent *getnetbyaddr(long, long);
  4679.  
  4680.  
  4681.    DESCRIPTION
  4682.          getnetbyname(), and getnetbyaddr() both return  a  pointer to
  4683.          an  object  with  the  following  structure  containing   the
  4684.          broken-out fields of a line in netdb configuration file.
  4685.  
  4686.  
  4687.            struct netent -
  4688.              char *n_name;       /* official name of net */
  4689.              char **n_aliases;   /* alias list */
  4690.              int  n_addrtype;    /* net number type */
  4691.              long n_net;         /* net number */
  4692.            ";
  4693.  
  4694.  
  4695.          The members of this structure are:
  4696.  
  4697.  
  4698.          n_name               The official name of the network.
  4699.  
  4700.  
  4701.          n_aliases           A  zero  terminated  list  of  alternate
  4702.                               names for the network.
  4703.  
  4704.  
  4705.          n_addrtype           The type of the network number returned;
  4706.                               currently only AF_INET.
  4707.  
  4708.  
  4709.          n_net                The network number.   Network numbers are
  4710.                               returned in machine byte order.
  4711.  
  4712.  
  4713.          Network numbers are supplied in host order.
  4714.  
  4715.  
  4716.          Type specifies the address type to use, currently only
  4717.          AF_INET is supported.
  4718.  
  4719.  
  4720.    DIAGNOSTICS
  4721.          A NULL pointer is returned if no matching entry was found or
  4722.          error occured.
  4723.  
  4724.  
  4725. System Manual                  AmiTCP/IP                 Section B.4    165
  4726.  
  4727.  
  4728.  
  4729.    BUGS
  4730.          All information is contained in a static area so it must  be
  4731.          copied ifit is to be saved.
  4732.  
  4733.  
  4734.          Only Internet network numbers are currently understood.
  4735.  
  4736.  
  4737.    SEE ALSO
  4738.          AmiTCP/IP configuration
  4739.  
  4740.  
  4741. 166    Section B.4                 AmiTCP/IP                  System Manual
  4742.  
  4743.  
  4744.  
  4745. B.4.3   getprotobyname()
  4746.  
  4747.    NAME
  4748.          getprotobyname, getprotobynumber - get protocol entry
  4749.  
  4750.  
  4751.    SYNOPSIS
  4752.          #include <netdb.h>
  4753.  
  4754.  
  4755.          protoent= getprotobyname(name)
  4756.          D0                         A0
  4757.  
  4758.  
  4759.          struct protoent *getprotobyname(char *);
  4760.  
  4761.  
  4762.          protoent= getprotobynumber(proto)
  4763.          D0                            D0
  4764.  
  4765.  
  4766.          struct protoent *getprotobynumber(long);
  4767.  
  4768.  
  4769.    DESCRIPTION
  4770.          getprotobyname() and getprotobynumber() both return a pointer
  4771.          to  an  object with the  following structure  containing  the
  4772.          broken-out fields of a line in netdb configuration file
  4773.  
  4774.  
  4775.            struct    protoent -
  4776.              char *p_name;       /* official name of protocol */
  4777.              char **p_aliases;   /* alias list */
  4778.              int  p_proto;       /* protocol number */
  4779.           ";
  4780.  
  4781.  
  4782.          The members of this structure are:
  4783.  
  4784.  
  4785.          p_name               The official name of the protocol.
  4786.          p_aliases           A  zero  terminated  list  of  alternate
  4787.                               names for the protocol.
  4788.          p_proto             The protocol number.
  4789.  
  4790.  
  4791.  
  4792.    DIAGNOSTICS
  4793.          A NULL pointer is returned if no matching entry was found or
  4794.          error occured.
  4795.  
  4796.  
  4797.    BUGS
  4798.          All information is contained in a static area so it must  be
  4799.          copied  if  it  is to be saved.  Only the Internet protocols
  4800.          are currently understood.
  4801.  
  4802.  
  4803.    SEE ALSO
  4804.          AmiTCP/IP configuration
  4805.  
  4806.  
  4807. System Manual                  AmiTCP/IP                 Section B.4    167
  4808.  
  4809.  
  4810.  
  4811. B.4.4   getservbyname()
  4812.  
  4813.  
  4814.  
  4815.    NAME
  4816.          getservbyname, getservbyport - get service entry
  4817.  
  4818.  
  4819.    SYNOPSIS
  4820.          #include <netdb.h>
  4821.  
  4822.  
  4823.          servent =getservbyname(name, proto)
  4824.          D0                       A0     A1
  4825.  
  4826.  
  4827.          struct servent *getservbyname(char *, char *)
  4828.  
  4829.  
  4830.          servent =getservbyport(port, proto)
  4831.          D0                       D0     A0
  4832.  
  4833.  
  4834.          struct servent *getservbyport(long, char *);
  4835.  
  4836.  
  4837.    DESCRIPTION
  4838.          getservbyname() and getservbyport() both return a pointer  to
  4839.          an   object  with  the  following  structure  containing  the
  4840.          broken-out fields of a line in netdb configuration file.
  4841.  
  4842.  
  4843.            struct    servent -
  4844.              char *s_name;       /* official name of service */
  4845.              char **s_aliases;   /* alias list */
  4846.              int  s_port;         /*port service resides at */
  4847.              char *s_proto;      /* protocol to use */
  4848.            ";
  4849.  
  4850.  
  4851.          The members of this structure are:
  4852.               s_name               The official name of the service.
  4853.               s_aliases            A zero terminated list of alternate
  4854.                                    names for the service.
  4855.               s_port               The port number at which  the  ser-
  4856.                                    vice  resides.    Port numbers  are
  4857.                                    returned  in  network  short   byte
  4858.                                    order.
  4859.               s_proto              The name of  the  protocol  to  use
  4860.                                    when contacting the service.
  4861.  
  4862.  
  4863.          The protoargument specifies the protocol for which to the
  4864.          sercive is to use. It is a normal C string, e.g. "tcp" or
  4865.          "udp".
  4866.  
  4867.  
  4868.    DIAGNOSTICS
  4869.          A NULL pointer is returned if no matching entry was found or
  4870.          error occured.
  4871.  
  4872.  
  4873.    BUGS
  4874.          All information is contained in a static area so it must  be
  4875.  
  4876.  
  4877. 168    Section B.4                 AmiTCP/IP                  System Manual
  4878.  
  4879.  
  4880.  
  4881.          copied  if it is to be saved.  Expecting port numbers to fit
  4882.          in a 32 bit quantity is probably naive.
  4883.  
  4884.  
  4885.    SEE ALSO
  4886.          AmiTCP/IP configuration
  4887.  
  4888.  
  4889. System Manual                  AmiTCP/IP                 Section B.5    169
  4890.  
  4891.  
  4892.  
  4893. B.5     AmiTCP/IP  Specific Extensions
  4894.  
  4895.  
  4896. B.5.1   Errno()
  4897.  
  4898.    NAME
  4899.          Errno - get error value after unsuccessful function call
  4900.  
  4901.  
  4902.    SYNOPSIS
  4903.          errno = Errno()
  4904.          D0
  4905.  
  4906.  
  4907.          LONG Errno(VOID);
  4908.  
  4909.  
  4910.    FUNCTION
  4911.          When  some  function  in  socket  library  return  an  error
  4912.          conditionvalue, they also set a specific error value.  This
  4913.          error value can be extracted by this function.
  4914.  
  4915.  
  4916.    RESULT
  4917.          Error value  indicating  the error on  last failure  of some
  4918.          socket function call.
  4919.  
  4920.  
  4921.    NOTES
  4922.          Return  value  of  Errno()  is not changed  after successful
  4923.          functionso so it cannot be used to determine success of any
  4924.          functioncall  of this library.  Also, another function call
  4925.          to this library may change  the return value of  Errno() so
  4926.          use it right after error occurred.
  4927.  
  4928.  
  4929.    SEE ALSO
  4930.          SetErrnoPtr()
  4931.  
  4932.  
  4933. 170    Section B.5                 AmiTCP/IP                  System Manual
  4934.  
  4935.  
  4936.  
  4937. B.5.2   ObtainSocket()
  4938.  
  4939.    NAME
  4940.          ObtainSocket - get a socket from AmiTCP/IP socket list
  4941.  
  4942.  
  4943.    SYNOPSIS
  4944.          s = ObtainSocket(id, domain, type, protocol)
  4945.          D0                D0  D1      D2     D3
  4946.  
  4947.  
  4948.          LONG ObtainSocket(LONG, LONG, LONG, LONG);
  4949.  
  4950.  
  4951.    FUNCTION
  4952.          When onetask wants to give  a socket to  an another one, it
  4953.          releasesit (with a key value) to a special socket list held
  4954.          by  AmiTCP/IP.    This  function  requests  that  socket  and
  4955.          receivesit if id and other parameters match.
  4956.  
  4957.  
  4958.    INPUTS
  4959.          id       - a key value given by the socket donator.
  4960.          domain   - see documentation of socket().
  4961.          type     - see documentation of socket().
  4962.          protocol- see documentation of socket().
  4963.  
  4964.  
  4965.    RESULT
  4966.          Non negative socket descriptor on success. On failure, -1 is
  4967.          returnedand the errno is set to indicate the error.
  4968.  
  4969.  
  4970.    ERRORS
  4971.          EMFILE          - The per-process descriptor table is
  4972.                             full.
  4973.  
  4974.  
  4975.          EPROTONOSUPPORT - The protocol type or the specified  pro-
  4976.                             tocol is not supported within this
  4977.                             domain.
  4978.  
  4979.  
  4980.          EPROTOTYPE       - The protocol is the wrong type for the
  4981.                             socket.
  4982.  
  4983.  
  4984.          EWOULDBLOCK      - Matching socket is not found.
  4985.  
  4986.  
  4987.    SEE ALSO
  4988.          ReleaseCopyOfSocket(), ReleaseSocket(), socket()
  4989.  
  4990.  
  4991. System Manual                  AmiTCP/IP                 Section B.5    171
  4992.  
  4993.  
  4994.  
  4995. B.5.3   ReleaseCopyOfSocket()
  4996.  
  4997.    NAME
  4998.          ReleaseCopyOfSocket - copy given socket to AmiTCP/IP socket list.
  4999.  
  5000.  
  5001.    SYNOPSIS
  5002.          id = ReleaseCopyOfSocket(fd, id)
  5003.          D0                        D0  D1
  5004.  
  5005.  
  5006.          LONG ReleaseCopyOfSocket(LONG, LONG);
  5007.  
  5008.  
  5009.    FUNCTION
  5010.          Make a new reference to a given socket (pointed by its descriptor)
  5011.          and release it to the socket list held by AmiTCP/IP.
  5012.  
  5013.  
  5014.    INPUTS
  5015.          fd - descriptor of the socket to release.
  5016.  
  5017.  
  5018.          id - thekey value to identify use of this socket. It can be
  5019.               unique or not, depending on its  value. If id value is
  5020.               between 0  and  65535,  inclusively,  it is  considered
  5021.               nonunique  and it can  be  used as a port  number,  for
  5022.               example.   If  id is greater  than  65535 and less than
  5023.               2^31) it  must be unique in currently held sockets  in
  5024.               AmiTCP/IP socket  list,  Otherwise  an  error will  be
  5025.               returned  and  socket  is  not  released.     If  id  ==
  5026.               UNIQUE_ID (defined in <sys/socket.h>) an unique id will
  5027.               be generated.
  5028.  
  5029.  
  5030.    RESULT
  5031.          id - -1 in case of error and the key value of the socket put
  5032.               in the list. Most useful when an uniqueid is generated
  5033.               by this routine.
  5034.  
  5035.  
  5036.    ERRORS
  5037.          EINVAL -Requested unique id is already used.
  5038.  
  5039.  
  5040.          ENOMEM -Needed memory couldn't be allocated.
  5041.  
  5042.  
  5043.    NOTE
  5044.          The socket descriptor is not deallocated.
  5045.  
  5046.  
  5047.    SEE ALSO
  5048.          ObtainSocket(), ReleaseSocket()
  5049.  
  5050.  
  5051. 172    Section B.5                 AmiTCP/IP                  System Manual
  5052.  
  5053.  
  5054.  
  5055. B.5.4   ReleaseSocket()
  5056.  
  5057.    NAME
  5058.          ReleaseSocket - release given socket to AmiTCP/IP socket list.
  5059.  
  5060.  
  5061.    SYNOPSIS
  5062.          id = ReleaseSocket(fd, id)
  5063.          D0                  D0  D1
  5064.  
  5065.  
  5066.          LONG ReleaseSocket(LONG, LONG);
  5067.  
  5068.  
  5069.    FUNCTION
  5070.          Release the reference of given socket (via  its  descriptor)
  5071.          and movethe socket to the  socket  list held by  AmiTCP/IP.
  5072.          The socket descriptor is deallocated in this procedure.
  5073.  
  5074.  
  5075.    INPUTS
  5076.          fd - descriptor of the socket to release.
  5077.  
  5078.  
  5079.          id - thekey value to identify use of this socket. It can be
  5080.               unique or not, depending on its  value. If id value is
  5081.               between 0  and  65535,  inclusively,  it is  considered
  5082.               nonunique  and it can  be  used as a port  number,  for
  5083.               example.   If  id is greater  than  65535 and less than
  5084.               2^31) it  must be unique in currently held sockets  in
  5085.               AmiTCP/IP socket  list,  Otherwise  an  error will  be
  5086.               returned  and  socket  is  not  released.     If  id  ==
  5087.               UNIQUE_ID (defined in <sys/socket.h>) an unique id will
  5088.               be generated.
  5089.  
  5090.  
  5091.    RESULT
  5092.          id - -1 in case of error and the key value of the socket put
  5093.               in the list. Most useful when an uniqueid is generated
  5094.               by this routine.
  5095.  
  5096.  
  5097.    ERRORS
  5098.          EINVAL -Requested unique id is already used.
  5099.  
  5100.  
  5101.          ENOMEM -Needed memory couldn't be allocated.
  5102.  
  5103.  
  5104.    SEE ALSO
  5105.          ObtainSocket(), ReleaseCopyOfSocket()
  5106.  
  5107.  
  5108. System Manual                  AmiTCP/IP                 Section B.5    173
  5109.  
  5110.  
  5111.  
  5112. B.5.5   SetDTableSize()
  5113.  
  5114.    NAME
  5115.          SetDTableSize - set socket descriptor table size of the base
  5116.  
  5117.  
  5118.    SYNOPSIS
  5119.          newsize =SetDTableSize(size)
  5120.          D0                       D0
  5121.  
  5122.  
  5123.          LONG SetDTableSize(UWORD);
  5124.  
  5125.  
  5126.    FUNCTION
  5127.          This  function increases  the  descriptor table size  inside
  5128.          library base so more sockets can be open at the same time.
  5129.  
  5130.  
  5131.    INPUT
  5132.          size - the new size of the desctiptor table.
  5133.  
  5134.  
  5135.    RESULT
  5136.          newsize -the new size of the descriptor table. Note that
  5137.          this canbe less than requested if an error occured.
  5138.  
  5139.  
  5140.    WARNING
  5141.          If the size of fd_set is not adjusted to store the increased
  5142.          space needed for  new  socket descriptors some other  memory
  5143.          will  bespilled.  Change  the  value of  FD_SETSIZE  before
  5144.          including any  socket  include  files  and  don't  increase
  5145.          descriptor  table    to    greater  than  the  new    value  of
  5146.          FD_SETSIZE.
  5147.  
  5148.  
  5149.    SEE ALSO
  5150.          getdtablesize(), select()
  5151.  
  5152.  
  5153. 174    Section B.5                 AmiTCP/IP                  System Manual
  5154.  
  5155.  
  5156.  
  5157. B.5.6   SetErrnoPtr()
  5158.  
  5159.    NAME
  5160.          SetErrnoPtr - set new place where the error value will be written
  5161.  
  5162.  
  5163.    SYNOPSIS
  5164.          SetErrnoPtr(ptr, size)
  5165.                       A0    D0
  5166.  
  5167.  
  5168.          VOID SetErrnoPtr(VOID *, UBYTE);
  5169.  
  5170.  
  5171.    FUNCTION
  5172.          This functions allows caller to redirect error variable inside
  5173.          scope of caller task.    Usually this is  used to make  task's
  5174.          global variable errno as error variable.
  5175.  
  5176.  
  5177.    INPUTS
  5178.          ptr     - pointer to error variable that isto be modified on
  5179.                    every error condition on this library function.
  5180.          size    - size of the error variable.
  5181.  
  5182.  
  5183.    EXAMPLE
  5184.          #include <errno.h>
  5185.  
  5186.  
  5187.          struct Library;
  5188.          struct Library * SocketBase = NULL;
  5189.  
  5190.  
  5191.          int main(void)
  5192.          -
  5193.             ...
  5194.            if ((SocketBase = OpenLibrary("bsdsocket.library", 2))
  5195.                != NULL) -
  5196.              SetErrnoPtr(&errno, sizeof(errno));
  5197.             ...
  5198.            "
  5199.          "
  5200.  
  5201.  
  5202.    NOTES
  5203.          Be sure that this new error variable exists until library base
  5204.          is finally closed or SetErrnoPtr() is called again for another
  5205.          variable.
  5206.  
  5207.  
  5208.    SEE ALSO
  5209.          Errno()
  5210.  
  5211.  
  5212. System Manual                  AmiTCP/IP                 Section B.5    175
  5213.  
  5214.  
  5215.  
  5216. B.5.7   SetSocketSignals()
  5217.  
  5218.    NAME
  5219.          SetSocketSignals - inform AmiTCP/IP of INTR, IO and URG signals
  5220.  
  5221.  
  5222.    SYNOPSIS
  5223.          SetSocketSignals(sigintrmask, sigiomask, sigurgmask)
  5224.                            D0            D1         D2
  5225.  
  5226.  
  5227.          VOID SetSocketSignals(ULONG, ULONG, ULONG);
  5228.  
  5229.  
  5230.    FUNCTION
  5231.          SetSocketSignals()  tells the AmiTCP/IP which  signal  masks
  5232.          corresponds UNIX SIGINT, SIGIO and SIGURG signals to be used
  5233.          in  this implementation.    sigintrmask  mask    is  used  to
  5234.          determine which  Amiga signals  interrupt  blocking library
  5235.          calls.   sigio-  and sigurgmasks  are sent when asynchronous
  5236.          notification of socket events is done and  when  out-of-band
  5237.          data arrives, respectively.
  5238.  
  5239.  
  5240.          Note that the supplied  values write over old ones. If this
  5241.          functionis used and CTRL-C is still wanted to interrupt the
  5242.          calls (the   default behaviour), the value BREAKF_CTRL_C must
  5243.          be explicitly given.
  5244.  
  5245.  
  5246.    SEE ALSO
  5247.          IoctlSocket(), recv(), send(), WaitSelect()
  5248.  
  5249.  
  5250.  
  5251.  
  5252.  
  5253. Appendix C
  5254.  
  5255.  
  5256.  
  5257. AmiTCP/IP  Network Link  Library
  5258.  
  5259.  
  5260.  
  5261. This appendix describes the functions located in the net.lib.
  5262.  
  5263.  
  5264.  
  5265. Table  of  Contents
  5266.  
  5267.  
  5268.         autoinit : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  5269.  * : : : : : : : : 177
  5270.         autoinitd : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :*
  5271.  * : : : :: : : : 178
  5272.         charRead : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  5273.  * : : : : : : : : 179
  5274.         gethostname : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  5275.  *: : : : : : : : 181
  5276.         lineRead : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : :*
  5277.  * : : : : : : : : 182
  5278.  
  5279.  
  5280.  
  5281.                                       176
  5282.  
  5283.  
  5284. System Manual                  AmiTCP/IP                 Section C.1    177
  5285.  
  5286.  
  5287.  
  5288. C.1     net.lib Functions
  5289.  
  5290.  
  5291. C.1.1   autoinit
  5292.  
  5293.    NAME
  5294.         autoinit - SAS C Autoinitialization Functions
  5295.  
  5296.  
  5297.    SYNOPSIS
  5298.         _STIopenSockets()
  5299.  
  5300.  
  5301.         void  _STIopenSockets(void)
  5302.  
  5303.  
  5304.         _STDcloseSockets()
  5305.  
  5306.  
  5307.         void  _STDcloseSockets(void)
  5308.  
  5309.  
  5310.    FUNCTION
  5311.         These functions open and close the bsdsocket.library at the
  5312.         startup and exit of the program, respectively. For a
  5313.         program to use these functions, it must be linked with
  5314.         netlib:net.lib.
  5315.  
  5316.  
  5317.         Ifthe library can be opened, the _STIopenSockets() calls
  5318.         bsdsocket.library function SetErrnoPtr() to tell the
  5319.         library the address and the size of the errno variable of
  5320.         thecalling program.
  5321.  
  5322.  
  5323.    NOTES
  5324.         _STIopenSockets() also checks that the system version is at
  5325.         least 37. It puts up a requester if the bsdsocket.library
  5326.         isnot found or is of wrong version.
  5327.  
  5328.  
  5329.         Theautoinitialization and autotermination functions are
  5330.         features specific to the SAS C6. However, these functions
  5331.         canbe used with other (ANSI) C compilers, too. Example
  5332.         follows:
  5333.  
  5334.  
  5335.         "*at start of main() *"
  5336.  
  5337.  
  5338.         atexit(_STDcloseSockets);
  5339.         _STDopenSockets();
  5340.  
  5341.  
  5342.    BUGS
  5343.  
  5344.  
  5345.    SEE ALSO
  5346.         bsdsocket.library/SetErrnoPtr(),
  5347.         SAS/C 6 User's Guide p. 145 for details of
  5348.         autoinitialization and autotermination functions.
  5349.  
  5350.  
  5351. 178    Section C.1                 AmiTCP/IP                  System Manual
  5352.  
  5353.  
  5354.  
  5355. C.1.2   autoinitd
  5356.  
  5357.    NAME
  5358.         autoinitd - SAS C Autoinitialization Functions for Daemons
  5359.  
  5360.  
  5361.    SYNOPSIS
  5362.         void  _STIopenSockets(void);
  5363.         void  _STDcloseSockets(void);
  5364.         long  server_socket;
  5365.  
  5366.  
  5367.    DESCRIPTION
  5368.         These are SASC autoinitialization functions for internet daemons
  5369.         started by inetd, Internet super-server. Upon startup, the server
  5370.         socket is obtained with ObtainSocket() library call. If successful,
  5371.         thesocket id is stored to the global variable server_socket. If the
  5372.         socket is not obtainable, the server_socket contains value -1.
  5373.         Ifthe server_socket is not valid, the server may try to accept() a
  5374.         newconnection and act as a stand-alone server.
  5375.  
  5376.  
  5377.    RESULT
  5378.         server_socket - positive socket id for success or -1 for failure.
  5379.  
  5380.  
  5381.    NOTES
  5382.         _STIopenSockets() also checks that the system version is at
  5383.         least 37. It puts up a requester if the bsdsocket.library
  5384.         isnot found or is of wrong version.
  5385.  
  5386.  
  5387.         Theautoinitialization and autotermination functions are
  5388.         features specific to the SAS C6. However, these functions
  5389.         canbe used with other (ANSI) C compilers, too. Example
  5390.         follows:
  5391.  
  5392.  
  5393.         "*at start of main() *"
  5394.  
  5395.  
  5396.         atexit(_STDcloseSockets);
  5397.         _STDopenSockets();
  5398.  
  5399.  
  5400.    AUTHOR
  5401.         Jarno Rajahalme, Pekka Pessi,
  5402.         theAmiTCP/IP Group <amitcp-group@hut.fi>,
  5403.         Helsinki University of Technology, Finland.
  5404.  
  5405.  
  5406.    SEE ALSO
  5407.         serveraccept(), netutil/inetd
  5408.  
  5409.  
  5410. System Manual                  AmiTCP/IP                 Section C.1    179
  5411.  
  5412.  
  5413.  
  5414. C.1.3   charRead
  5415.  
  5416.  
  5417.    NAME
  5418.         charRead -- read characters from socket one by one.
  5419.  
  5420.  
  5421.    SYNOPSIS
  5422.         initCharRead(rc, fd)
  5423.  
  5424.  
  5425.         void initCharRead(struct CharRead *, int);
  5426.  
  5427.  
  5428.  
  5429.         character = charRead(rc)
  5430.  
  5431.  
  5432.         intcharRead(struct CharRead *);
  5433.  
  5434.  
  5435.  
  5436.    DESCRIPTION
  5437.         charRead is a macro package which return characters one by one
  5438.         from given socket input stream. The socket where data is to be read
  5439.         isset by calling initCharRead(): rc is the pointer to charread
  5440.         structure previously allocated. fd is the (socket) descriptor where
  5441.         reading is to be done.
  5442.  
  5443.  
  5444.         charRead() returns the next character from input stream or one of
  5445.         the following:
  5446.  
  5447.  
  5448.         RC_DO_SELECT     (-3)     - read input buffer is returned. Do select
  5449.                                    before next call if you don't want charread
  5450.                                    to  block.
  5451.  
  5452.  
  5453.         RC_EOF           (-2)    - end-of-file condition has occurred.
  5454.  
  5455.  
  5456.         RC_ERROR         (-1)    - there has been an error while filling new
  5457.                                    charread buffer. Check the value of Errno()
  5458.  
  5459.  
  5460.    NOTE
  5461.         Always use variable of type int to store return value from charRead()
  5462.         since the numeric value of characters returned may vary between
  5463.         0 -255 (or even greater). As you may know, -3 equals 253 if of type
  5464.         unsigned char.
  5465.  
  5466.  
  5467.    EXAMPLE
  5468.         /*
  5469.          * This piece of code shows how to use charread with select()
  5470.          */
  5471.         #include <sys/types.h>
  5472.         #include <sys/socket.h>
  5473.         #include <charread.h>
  5474.  
  5475.  
  5476.         main_loop(int sock)
  5477.         -
  5478.           struct CharReadrc;
  5479.           fd_set readfds;
  5480.           int c;
  5481.  
  5482.  
  5483.           initCharRead(&rc,  sock);
  5484.  
  5485.  
  5486. 180    Section C.1                 AmiTCP/IP                  System Manual
  5487.  
  5488.  
  5489.  
  5490.           FD_ZERO(&readfds);
  5491.  
  5492.  
  5493.           while(1) -
  5494.             FD_SET(sock, &readfds);
  5495.  
  5496.  
  5497.             if (select(sock + 1. &readfds, NULL, NULL, NULL)) < 0) -
  5498.               perror("select");
  5499.               break;
  5500.             "
  5501.             if (FD_ISSET(sock, &readfds)) -
  5502.               while((c = charRead(&rc)) >= 0)
  5503.                 handle_next_input_character(c);
  5504.               if (c == RC_EOF)
  5505.                 break;
  5506.               if (c == RC_ERROR) -
  5507.                 perror("charRead");
  5508.                 break;
  5509.               "
  5510.             "
  5511.           "
  5512.         "
  5513.  
  5514.  
  5515.     PORTABILITY
  5516.         Thesource file charread.h should be able to be used in
  5517.         UNIX programs as is.
  5518.  
  5519.  
  5520.     AUTHORS
  5521.         Tomi  Ollila,
  5522.         theAmiTCP/IP Group <amitcp-group@hut.fi>,
  5523.  
  5524.  
  5525.     SEE ALSO
  5526.         lineRead(), bsdsocket.library/recv()
  5527.  
  5528.  
  5529. System Manual                  AmiTCP/IP                 Section C.1    181
  5530.  
  5531.  
  5532.  
  5533. C.1.4   gethostname
  5534.  
  5535.    NAME
  5536.         gethostname -- get the name of the host
  5537.  
  5538.  
  5539.    SYNOPSIS
  5540.         error = gethostname(name, namelen);
  5541.  
  5542.  
  5543.         intgethostname(char *, int);
  5544.  
  5545.  
  5546.    FUNCTION
  5547.         Getthe name of the host to the buffer name of length namelen.
  5548.         Thename is taken from the environment variable "HOSTNAME"
  5549.         where it SHOULD reside.
  5550.  
  5551.  
  5552.    INPUTS
  5553.         name     - Pointer to the buffer where the name should be
  5554.                   stored.
  5555.         namelen - Length of the buffer name.
  5556.  
  5557.  
  5558.    RESULT
  5559.         error    - 0 on success,-1 in case of an error. The global
  5560.                   variable errno will be set to indicate the error as
  5561.                   follows:
  5562.  
  5563.  
  5564.                   ENOENT - The environment variable "HOSTNAME" is not
  5565.                             found.
  5566.  
  5567.  
  5568.    EXAMPLE
  5569.         char  hostname[MAXHOSTNAMELEN];
  5570.         int error;
  5571.  
  5572.  
  5573.         error = gethostname(hostname, sizeof(hostname));
  5574.         if(error < 0)
  5575.           exit(10);
  5576.  
  5577.  
  5578.         printf("My name is ""%s""."n", hostname);
  5579.  
  5580.  
  5581.    NOTES
  5582.         This function is included for source compatibility with Unix
  5583.         systems.
  5584.         TheENOENT errno value is AmiTCP/IP addition.
  5585.  
  5586.  
  5587.    BUGS
  5588.         Unlike the Unix version, this version assures that the
  5589.         resulting string is always NULL-terminated.
  5590.  
  5591.  
  5592.    SEE ALSO
  5593.         getenv()
  5594.  
  5595.  
  5596. 182    Section C.1                 AmiTCP/IP                  System Manual
  5597.  
  5598.  
  5599.  
  5600. C.1.5   lineRead
  5601.  
  5602.  
  5603.    NAME
  5604.         lineRead -- read newline terminated strings from socket
  5605.  
  5606.  
  5607.    SYNOPSIS
  5608.         initLineRead(rl, fd, lftype, bufsize)
  5609.  
  5610.  
  5611.         void initLineRead(struct LineRead *, int, int, int);
  5612.  
  5613.  
  5614.  
  5615.         length = lineRead(rl)
  5616.  
  5617.  
  5618.         intlineread(struct LineRead *);
  5619.  
  5620.  
  5621.  
  5622.    DESCRIPTION
  5623.         lineRead() reads newline terminated strings from given descriptor
  5624.         very efficiently. All the options needed are set by calling
  5625.         initLineRead(): rl is the pointer to lineread structure previously
  5626.         allocated. fd is the (socket) descriptor where reading is to be
  5627.         done. lftype can have following 3 values:
  5628.  
  5629.  
  5630.             RL_LFNOTREQ - Newline terminated strings are returned unless
  5631.                            there is no newlines left in currently buffered
  5632.                            input. In this case remaining buffer is returned.
  5633.  
  5634.  
  5635.             RL_LFREQLF  - If there is no newlines left in currently buffered
  5636.                            input the remaining input datais copied at the
  5637.                            start of buffer. Caller is informed that next
  5638.                            call will fill the buffer (andit may block).
  5639.                            Lines are always returned withnewline at the end
  5640.                            unless the string is longer than whole buffer.
  5641.  
  5642.  
  5643.             RL_LFREQNUL  - Like LF_REQLF, but remaining newline is removed.
  5644.                            Note here that lenght is one longer that actual
  5645.                            string length since line that has only one
  5646.                            newline at the end would returnlength as 0
  5647.                            which indigate string incomplete condition.
  5648.  
  5649.  
  5650.         bufsize is used to tell lineread how big the receive buffer is.
  5651.         always put RL_BUFSIZE here since that value is used to determine
  5652.         thememory allocated for the buffer. This option is given to you
  5653.         soyou may decide to use different buffer size than the default
  5654.         1024.
  5655.  
  5656.  
  5657.         lineRead() returns the newline terminated string in rl_line field
  5658.         oflineread structure. Return values of lineRead() are:
  5659.  
  5660.  
  5661.              1 - RL_BUFSIZE     - normal length of returned string.
  5662.  
  5663.  
  5664.              0                   - If zero is returned just after select(),
  5665.                                    end-of-file condition has occurred.
  5666.                                    Otherwise string is not completed yet.
  5667.                                    Make sure you call select() (or use non-
  5668.                                    blocking IO) if you don't want next call
  5669.  
  5670.  
  5671. System Manual                  AmiTCP/IP                 Section C.1    183
  5672.  
  5673.  
  5674.  
  5675.                                    to  block.
  5676.  
  5677.  
  5678.             -1                   - if rl_Line field of lineread structure
  5679.                                    is NULL, it indicates error condition.
  5680.                                    If rl_Line points to start of string
  5681.                                    buffer, input string has been longer
  5682.                                    than buffer. In this case rl_Line points
  5683.                                    to zero terminated string of length
  5684.                                    RL_BUFSIZE.
  5685.  
  5686.  
  5687.         Youmay modify the zero terminated string returned by lineRead() in
  5688.         anyway, but memory around the string is private lineread memory.
  5689.  
  5690.  
  5691.    EXAMPLE
  5692.         /*
  5693.          * The following code shows how to use lineread with select()
  5694.          */
  5695.         #ifdef  USE_LOW_MEMORY_BUFFER
  5696.         #define RL_BUFSIZE 256
  5697.         #endif
  5698.  
  5699.  
  5700.         #include <sys/types.h>
  5701.         #ifdef  AMIGA
  5702.         #include <bsdsocket.h>
  5703.         #endif
  5704.         #include <lineread.h>
  5705.  
  5706.  
  5707.         #define NULL 0
  5708.  
  5709.  
  5710.         ...
  5711.  
  5712.  
  5713.         main_loop(int sock)
  5714.         -
  5715.           struct LineRead* rl;
  5716.           int length;
  5717.           fd_set reafdfs;
  5718.  
  5719.  
  5720.           if (rl = (struct LineRead *)AllocMem(sizeof (*rl), 0)) -
  5721.  
  5722.  
  5723.             initLineRead(rl, sock, LF_REQLF, RL_BUFSIZE);
  5724.  
  5725.  
  5726.             FD_ZERO(&readfds);
  5727.  
  5728.  
  5729.             while(1) -
  5730.               FD_SET(sock, &readfds);
  5731.  
  5732.  
  5733.               if (select(sock + 1, &readfds, NULL, NULL, NULL)) < 0) -
  5734.                 perror("select");
  5735.                 break;
  5736.               "
  5737.               if (FD_ISSET(sock, &readfds))
  5738.                 if ((length = lineRead(rl)) == 0) /* EOF */
  5739.                   break;
  5740.                 do -
  5741.                   if (length > 0)
  5742.  
  5743.  
  5744. 184    Section C.1                 AmiTCP/IP                  System Manual
  5745.  
  5746.  
  5747.  
  5748.                     write(1, rl->rl_Line, length); /* stdout. write() for */
  5749.                                                       /* speed demonstration*/
  5750.                   else - /* length == -1 */
  5751.                     if (rl->rl_Line == NULL); -
  5752.                        perror("lineRead");
  5753.                        break;
  5754.                     "
  5755.                     else -
  5756.                        fprintf(stderr, "lineread input buffer overflow!"n");
  5757.                        write(1, rl->rl_Line, RL_BUFSIZE);
  5758.                        write(1, ""n", 1);
  5759.                     "
  5760.                   "
  5761.                 " while ((length = lineRead(rl)) != 0); /* 0 -> doselect() */
  5762.             "
  5763.           FreeMem(rl, sizeof (*rl);
  5764.           "
  5765.           else
  5766.             fprintf("AllocMem: Out Of memory"n");
  5767.         "
  5768.  
  5769.  
  5770.     PORTABILITY
  5771.         Thesource modules lineread.c and lineread.h should compile
  5772.         inUNIX machines as is.
  5773.  
  5774.  
  5775.     AUTHORS
  5776.         Tomi  Ollila,
  5777.         theAmiTCP/IP Group <amitcp-group@hut.fi>,
  5778.  
  5779.  
  5780.     SEE ALSO
  5781.         readChar(), bsdsocket.library/recv()
  5782.  
  5783.  
  5784.  
  5785.  
  5786.  
  5787. Appendix D
  5788.  
  5789.  
  5790.  
  5791. Protocols  and  Network Interfaces
  5792.  
  5793.  
  5794.  
  5795. The AutoDoc file protocol.doc contains on-line manual pages for protocols
  5796. and network interfaces.
  5797.  
  5798.  
  5799.  
  5800. Table  of  Contents
  5801.  
  5802.  
  5803.         arp : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : *
  5804.  *: : : : : : : : : : 186
  5805.         icmp : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : :*
  5806.  * : : : : : : : : : 189
  5807.         if  : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : *
  5808.  *: : : : : : : : : : 190
  5809.         inet : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : :*
  5810.  * : : : : : : : : : 193
  5811.         ip  : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : *
  5812.  *: : : : : : : : : : 196
  5813.         lo  : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : *
  5814.  *: : : : : : : : : : 198
  5815.         routing : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : *
  5816.  *: : : : : : : : : 199
  5817.         tcp : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : *
  5818.  *: : : : : : : : : : 201
  5819.         udp : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : *
  5820.  *: : : : : : : : : : 203
  5821.  
  5822.  
  5823.  
  5824.                                       185
  5825.  
  5826.  
  5827. 186    Section D.1                 AmiTCP/IP                  System Manual
  5828.  
  5829.  
  5830.  
  5831. D.1     Protocols  and  Network  Interfaces
  5832.  
  5833.  
  5834.  
  5835. D.1.1   arp
  5836.  
  5837.  
  5838.    NAME
  5839.         arp- Address Resolution Protocol
  5840.  
  5841.  
  5842.    CONFIG
  5843.         AnySANA-II device driver using ARP
  5844.  
  5845.  
  5846.    SYNOPSIS
  5847.         #include <sys/socket.h>
  5848.         #include <net/if_arp.h>
  5849.         #include <netinet/in.h>
  5850.  
  5851.  
  5852.         s =socket(AF_INET, SOCK_DGRAM, 0);
  5853.  
  5854.  
  5855.    DESCRIPTION
  5856.         ARPis a protocol used to dynamically map between Internet
  5857.         Protocol (IP) and hardware addresses. It can be used by most
  5858.         theSANA-II network interface drivers. The current
  5859.         implementation supports only Internet Protocol (and is tested
  5860.         only with Ethernet).  However, ARP is not limited to only that
  5861.         combination.
  5862.  
  5863.  
  5864.         ARPcaches IP-to-hardware address mappings. When an interface
  5865.         requests a mapping for an address not in the cache, ARP queues
  5866.         themessage which requires the mapping and broadcasts a
  5867.         message on the associated network requesting the address
  5868.         mapping. If a response is provided, the new mapping is cached
  5869.         andany pending message is transmitted. ARP will queue at most
  5870.         onepacket while waiting for a mapping request to be responded
  5871.         to;only the most recently transmitted packet is kept.
  5872.  
  5873.  
  5874.         Theaddress mapping caches are separate for each interface. The
  5875.         amount of mappings in the cache may be specified with an
  5876.         IoctlSocket() request.
  5877.  
  5878.  
  5879.         Tofacilitate communications with systems which do not use ARP,
  5880.         IoctlSocket() requests are provided to enter and delete entries
  5881.         inthe IP-to-Ethernet tables.
  5882.  
  5883.  
  5884.    USAGE
  5885.         #include <sys/ioctl.h>
  5886.         #include <sys/socket.h>
  5887.         #include <net/if.h>
  5888.         #include <net/if_arp.h>
  5889.  
  5890.  
  5891.         struct arpreq arpreq;
  5892.  
  5893.  
  5894.         IoctlSocket(s, SIOCSARP, (caddr_t)&arpreq);
  5895.         IoctlSocket(s, SIOCGARP, (caddr_t)&arpreq);
  5896.         IoctlSocket(s, SIOCDARP, (caddr_t)&arpreq);
  5897.  
  5898.  
  5899.         These three IoctlSocket()s take the same structure as an argument.
  5900.  
  5901.  
  5902. System Manual                  AmiTCP/IP                 Section D.1    187
  5903.  
  5904.  
  5905.  
  5906.         SIOCSARP sets an ARP entry, SIOCGARP gets an ARP entry, and SIOCDARP
  5907.         deletes an ARP entry. These IoctlSocket() requests may be applied to
  5908.         anysocket descriptor (s). The arpreq structure contains:
  5909.  
  5910.  
  5911.         /*Maximum number of octets in protocol/hw address */
  5912.         #define  MAXADDRARP   16
  5913.  
  5914.  
  5915.         /*
  5916.          * ARP ioctl request.
  5917.          */
  5918.         struct arpreq -
  5919.                 struct  sockaddr arp_pa;  /* protocol address */
  5920.                 struct  -                  /* hardware address */
  5921.                   u_char sa_len;          /* actual length + 2 */
  5922.                   u_char sa_family;
  5923.                   char   sa_data[MAXADDRARP];
  5924.                 "  arp_ha;
  5925.                 int     arp_flags;                     /* flags */
  5926.         ";
  5927.  
  5928.  
  5929.         /*  arp_flags andat_flags field values */
  5930.         #define ATF_INUSE        0x01          /* entry in use */
  5931.         #define ATF_COM         0x02        /* completed entry */
  5932.         #define ATF_PERM        0x04        /* permanent entry */
  5933.         #define ATF_PUBL         0x08          /* publish entry */
  5934.  
  5935.  
  5936.  
  5937.         Theinterface whose ARP table is manipulated is specified by
  5938.         arp_pa sockaddr. The address family for the arp_pa sockaddr
  5939.         must be AF_INET; for the arp_ha sockaddr it must be AF_UNSPEC.
  5940.         Thelength of arp_ha must match the length of used hardware
  5941.         address. Maximum length for the hardware address is MAXADDRARP
  5942.         bytes. The only flag bits which may be written are ATF_PERM
  5943.         andATF_PUBL. ATF_PERM makes the entry permanent if the
  5944.         IoctlSocket() call succeeds. ATF_PUBL specifies that the ARP
  5945.         code should respond to ARP requests for the indicated host
  5946.         coming from other machines.  This allows a host to act as an
  5947.         ARPserver which may be useful in convincing an ARP-only
  5948.         machine to talk to a non-ARP machine.
  5949.  
  5950.  
  5951.    UNSUPPORTED IN AmiTCP/IP
  5952.  
  5953.  
  5954.    AmiTCP/IP EXTENSIONS
  5955.         There is an extension to the standard BSD4.4 ARP ioctl interface to
  5956.         access the contents of the whole ARP mapping cache. (In the BSD4.4
  5957.         thestatic ARP table is accessed via the /dev/kmem.) The SIOCGARPT
  5958.         ioctl takes the following arptabreq structure as an argument:
  5959.  
  5960.  
  5961.         /*
  5962.          * An AmiTCP/IP specific ARP table ioctl request
  5963.          */
  5964.         struct arptabreq -
  5965.                 struct arpreq atr_arpreq;  /* To identify the interface */
  5966.                 long   atr_size;           /* # of elements in atr_table */
  5967.                  long   atr_inuse;                 /* # of elements in use */
  5968.  
  5969.  
  5970. 188    Section D.1                 AmiTCP/IP                  System Manual
  5971.  
  5972.  
  5973.  
  5974.                 struct arpreq *atr_table;
  5975.         ";
  5976.  
  5977.  
  5978.         Theatr_arpreq specifies the used interface. The hardware address
  5979.         forthe interface is returned in the arp_ha field of atr_arpreq
  5980.         structure.
  5981.  
  5982.  
  5983.         TheSIOCGARPT ioctl reads at most atr_size entries from the cache
  5984.         into the user supplied buffer atr_table, if it is not NULL. Actual
  5985.         amount of returned entries is returned in atr_size. The current
  5986.         amount of cached mappings is returned in the atr_inuse.
  5987.  
  5988.  
  5989.         TheSIOCGARPT ioctl has following usage:
  5990.  
  5991.  
  5992.         struct arpreq cache[N];
  5993.         struct arptabreq arptab = - N, 0, cache ";
  5994.  
  5995.  
  5996.         IoctlSocket(s, SIOCGARPT, (caddr_t)&arptabreq);
  5997.  
  5998.  
  5999.    DIAGNOSTICS
  6000.         ARPwatches passively for hosts impersonating the local host
  6001.         (that  is,  a  host which responds to an ARP mapping request
  6002.         forthe local host's address).
  6003.  
  6004.  
  6005.         "duplicate IP address a.b.c.d!!"
  6006.         "sent from hardware address: %x:%x:...:%x:%x"
  6007.  
  6008.  
  6009.         ARP has  discovered  another host on the local network
  6010.         which responds to mapping requests for its own Internet
  6011.         address.
  6012.  
  6013.  
  6014.    BUGS
  6015.         TheARP is tested only with Ethernet. Other network hardware may
  6016.         require special ifconfig configuration.
  6017.  
  6018.  
  6019.    SEE ALSO
  6020.         inet, netutil/arp, netutil/ifconfig, <net/if_arp.h>
  6021.  
  6022.  
  6023.         Plummer, Dave, ``An  Ethernet  Address  Resolution  Protocol
  6024.         -or-   ConvertingNetwork Protocol Addresses to 48.bit Ether-
  6025.         netAddresses for Transmission on Ethernet  Hardware,''  RFC
  6026.         826,  Network  Information  Center, SRI International, Menlo
  6027.         Park, Calif., November 1982. (Sun 800-1059-10)
  6028.  
  6029.  
  6030. System Manual                  AmiTCP/IP                 Section D.1    189
  6031.  
  6032.  
  6033.  
  6034. D.1.2   icmp
  6035.  
  6036.    NAME
  6037.         icmp - Internet Control Message Protocol
  6038.  
  6039.  
  6040.    SYNOPSIS
  6041.         #include <sys/socket.h>
  6042.         #include <netinet/in.h>
  6043.  
  6044.  
  6045.         int
  6046.         socket(AF_INET, SOCK_RAW, proto)
  6047.  
  6048.  
  6049.    DESCRIPTION
  6050.         ICMP is the error and control message protocol used by IP and the
  6051.         Internet protocol family.  It may be accessed through a ``raw
  6052.         socket'' for network monitoring and diagnostic functions.  The proto
  6053.         parameter to the socket call to create an ICMP socket is obtained
  6054.         from getprotobyname().  ICMP sockets are connectionless, and are
  6055.         normally used with the sendto() and recvfrom() calls, though the
  6056.         connect() call may also be used to fix the destination for future
  6057.         packets (in which case the recv() and send() socket library calls
  6058.         maybe used).
  6059.  
  6060.  
  6061.         Outgoing packets automatically have an IP header prepended to them
  6062.         (based on the destination address).  Incoming packets are received
  6063.         with the IP header and options intact.
  6064.  
  6065.  
  6066.    DIAGNOSTICS
  6067.         A socket operation may fail with one of the following errors
  6068.         returned:
  6069.  
  6070.  
  6071.         [EISCONN]         when trying to establish a connection ona socket
  6072.                           which already has one, orwhen trying to send a
  6073.                           datagram with the destination address specified and
  6074.                           the socket is already connected;
  6075.  
  6076.  
  6077.         [ENOTCONN]        when trying tosend a datagram, but no destination
  6078.                           address is specified, andthe socket hasn't been
  6079.                           connected;
  6080.  
  6081.  
  6082.         [ENOBUFS]         when the system runs out of memory for aninternal
  6083.                           data structure;
  6084.  
  6085.  
  6086.         [EADDRNOTAVAIL]  when an attempt is made to create a socket with a
  6087.                           network address for whichno network interface
  6088.                           exists.
  6089.  
  6090.  
  6091.    SEE ALSO
  6092.         bsdsocket.library/send(),  bsdsocket.library/recv(), inet,  ip
  6093.  
  6094.  
  6095.    HISTORY
  6096.         Theicmp protocol is originally from 4.3BSD.
  6097.  
  6098.  
  6099. 190    Section D.1                 AmiTCP/IP                  System Manual
  6100.  
  6101.  
  6102.  
  6103. D.1.3   if
  6104.  
  6105.  
  6106.    NAME
  6107.         if- Network Interface to SANA-II devices
  6108.  
  6109.  
  6110.    DESCRIPTION
  6111.         Each network interface in the AmiTCP/IP corresponds to a path
  6112.         through which messages may be sent and received.  A network
  6113.         interface usually has a SANA-II device driver associated with it,
  6114.         though the loopback interface, "lo", do not. The network interface
  6115.         inthe AmiTCP/IP (sana_softc) is superset of the BSD Unix network
  6116.         interface.
  6117.  
  6118.  
  6119.         When the network interface is first time referenced, AmiTCP/IP tries
  6120.         toopen the corresponding SANA-II device driver. If successful, a
  6121.         software interface to the SANA-II device is created. The "network/"
  6122.         prefix is added to the SANA-II device name, if needed. Once the
  6123.         interface has acquired its address, it is expected to install a
  6124.         routing table entry so that messages can be routed through it.
  6125.  
  6126.  
  6127.         TheSANA-II interfaces must be configured before they will allow
  6128.         traffic to flow through them. It is done after the interface is
  6129.         assigned a protocol address with a SIOCSIFADDR ioctl. Some
  6130.         interfaces may use the protocol address or a part of it as their
  6131.         hardware address. On interfaces where the network-link layer address
  6132.         mapping is static, only the network number is taken from the ioctl;
  6133.         theremainder is found in a hardware specific manner. On interfaces
  6134.         which provide dynamic network-link layer address mapping facilities
  6135.         (for example, Ethernets or Arcnets using ARP), the entire address
  6136.         specified in the ioctl is used.
  6137.  
  6138.  
  6139.         Thefollowing ioctl calls may be used to manipulate network
  6140.         interfaces. Unless specified otherwise, the request takes an ifreq
  6141.         structure as its parameter. This structure has the form
  6142.  
  6143.  
  6144.         struct ifreq -
  6145.           char ifr_name[IFNAMSIZ]; /* interface name (eg. "slip.device/0")*/
  6146.           union -
  6147.             struct sockaddr ifru_addr;
  6148.             struct sockaddr ifru_dstaddr;
  6149.             short            ifru_flags;
  6150.           " ifr_ifru;
  6151.         #define ifr_addr     ifr_ifru.ifru_addr                   /* address */
  6152.         #define ifr_dstaddr ifr_ifru.ifru_dstaddr   /* end of p-to-p link */
  6153.         #define ifr_flags    ifr_ifru.ifru_flags                    /* flags */
  6154.         ";
  6155.  
  6156.  
  6157.         SIOCSIFADDR       Set interface address. Following the address
  6158.                           assignment, the ``initialization'' routine for
  6159.                           the interface is called.
  6160.  
  6161.  
  6162.         SIOCGIFADDR       Get interface address.
  6163.  
  6164.  
  6165.         SIOCSIFDSTADDR    Set point to point address for interface.
  6166.  
  6167.  
  6168.         SIOCGIFDSTADDR    Get point to point address for interface.
  6169.  
  6170.  
  6171. System Manual                  AmiTCP/IP                 Section D.1    191
  6172.  
  6173.  
  6174.  
  6175.         SIOCSIFFLAGS      Set interface flagsfield. If the interface is
  6176.                           marked down, any processes currently routing
  6177.                           packets through the interface are notified.
  6178.  
  6179.  
  6180.         SIOCGIFFLAGS      Get interface flags.
  6181.  
  6182.  
  6183.         SIOCGIFCONF       Get interface configurationlist. This request
  6184.                           takes an ifconf structure(see below) as a
  6185.                           value-result parameter. The ifc_len field should be
  6186.                           initially set to the sizeof the buffer pointed to
  6187.                           by ifc_buf. On return itwill contain the length,
  6188.                           in bytes, of the configuration list.
  6189.  
  6190.  
  6191.         /*
  6192.          * Structure used in SIOCGIFCONF request.
  6193.          * Used toretrieve interface configuration
  6194.          * for machine (useful for programs which
  6195.          * must know all networks accessible).
  6196.          */
  6197.         struct ifconf -
  6198.           int  ifc_len;                       /* size of associated buffer */
  6199.           union -
  6200.             caddr_t       ifcu_buf;
  6201.             struct ifreq *ifcu_req;
  6202.           " ifc_ifcu;
  6203.         #define ifc_buf ifc_ifcu.ifcu_buf                /* buffer address */
  6204.         #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
  6205.         ";
  6206.  
  6207.  
  6208.  
  6209.    UNSUPPORTED IN AmiTCP/IP
  6210.         These standard BSD ioctl codes are not currently supported:
  6211.  
  6212.  
  6213.         SIOCADDMULTI      Enable a multicast address for the interface.
  6214.  
  6215.  
  6216.         SIOCDELMULTI      Disable a previouslyset multicast address.
  6217.  
  6218.  
  6219.         SIOCSPROMISC      Toggle promiscuous mode.
  6220.  
  6221.  
  6222.    AmiTCP/IP EXTENSIONS
  6223.         Thefollowing ioctls are used to configure protocol and hardware
  6224.         specific properties of a sana_softc interface. They are used in the
  6225.         AmiTCP/IP only.
  6226.  
  6227.  
  6228.         SIOCSSANATAGS     Set SANA-II specific properties with a tag list.
  6229.  
  6230.  
  6231.         SIOCGSANATAGS     Get SANA-II specific properties into a
  6232.                           wiretype_parameters structure and a user tag list.
  6233.  
  6234.  
  6235.         struct  wiretype_parameters
  6236.         -
  6237.           ULONG  wiretype;                /* the wiretype of theinterface */
  6238.           WORD   flags;                                         /* iff_flags*/
  6239.           struct TagItem*tags;                   /* tag list userprovides */
  6240.  
  6241.  
  6242. 192    Section D.1                 AmiTCP/IP                  System Manual
  6243.  
  6244.  
  6245.  
  6246.         ";
  6247.  
  6248.  
  6249.    SEE ALSO
  6250.         arp, lo, netutil/arp, netutil/ifconfig, <sys/ioctl.h>, <net/if.h>,
  6251.         <net/sana2tags.h>
  6252.  
  6253.  
  6254. System Manual                  AmiTCP/IP                 Section D.1    193
  6255.  
  6256.  
  6257.  
  6258. D.1.4   inet
  6259.  
  6260.  
  6261.    NAME
  6262.         inet - Internet protocol family
  6263.  
  6264.  
  6265.    SYNOPSIS
  6266.         #include <sys/types.h>
  6267.         #include <netinet/in.h>
  6268.  
  6269.  
  6270.    DESCRIPTION
  6271.         TheInternet protocol family implements a collection of protocols
  6272.         which are centered around the Internet Protocol (IP) and which share
  6273.         a common address format.  The Internet family provides protocol
  6274.         support for the SOCK_STREAM, SOCK_DGRAM, and SOCK_RAW socket types.
  6275.  
  6276.  
  6277.    PROTOCOLS
  6278.         TheInternet protocol family is comprised of the Internet Protocol
  6279.         (IP), the Address Resolution Protocol (ARP), the Internet Control
  6280.         Message Protocol (ICMP), the Transmission Control Protocol (TCP),
  6281.         andthe User Datagram Protocol (UDP).
  6282.  
  6283.  
  6284.         TCPis used to support the SOCK_STREAM abstraction while UDP is used
  6285.         tosupport the SOCK_DGRAM abstraction; (SEE ALSO tcp, SEE ALSO udp).
  6286.         A raw interface to IP is available by creating an Internet socket of
  6287.         type SOCK_RAW; (SEE ALSO ip).  ICMP is used by the kernel to handle
  6288.         andreport errors in protocol processing.  It is also accessible to
  6289.         user programs; (SEE ALSO icmp).  ARP is used to translate 32-bit IP
  6290.         addresses into varying length hardware addresses; (SEE ALSO arp).
  6291.  
  6292.  
  6293.         The32-bit IP address is divided into network number and host number
  6294.         parts.   It is frequency-encoded; the most significant bit is zero in
  6295.         Class A addresses, in which the high-order 8 bits are the network
  6296.         number.   Class Baddresses have their high order two bits set to 10
  6297.         anduse the highorder 16 bits as the network number field.  Class C
  6298.         addresses have a 24-bit network number part of which the high order
  6299.         three bits are 110.  Sites with a cluster of local networks may
  6300.         chose to use a single network number for the cluster; this is done
  6301.         byusing subnet addressing.  The local (host) portion of the address
  6302.         isfurther subdivided into subnet number and host number parts.
  6303.         Within a subnet, each subnet appears to be an individual network;
  6304.         externally, the entire cluster appears to be a single, uniform
  6305.         network requiring only a single routing entry.  Subnet addressing is
  6306.         enabled and examined by the following ioctl commands on a datagram
  6307.         socket in the Internet domain; they have the same form as the
  6308.         SIOCIFADDR (SEE ALSO if) command.
  6309.  
  6310.  
  6311.         SIOCSIFNETMASK       Set interface network mask.   The network mask
  6312.                              defines the network part of the address; ifit
  6313.                              contains more of the address than the address
  6314.                              type would indicate, then subnets are in use.
  6315.  
  6316.  
  6317.         SIOCGIFNETMASK       Get interface network mask.
  6318.  
  6319.  
  6320.    ADDRESSING
  6321.         IPaddresses are four byte quantities, stored in network byte order
  6322.         (the native Amiga byte order)
  6323.  
  6324.  
  6325. 194    Section D.1                 AmiTCP/IP                  System Manual
  6326.  
  6327.  
  6328.  
  6329.         Sockets in the Internet protocol family  use  the  following
  6330.         addressing structure:
  6331.              struct sockaddr_in -
  6332.                   short      sin_family;
  6333.                   u_short   sin_port;
  6334.                   struct     in_addr sin_addr;
  6335.                   char sin_zero[8];
  6336.              ";
  6337.  
  6338.  
  6339.         Functions in bsdsocket.library are provided to manipulate structures
  6340.         ofthis form.
  6341.  
  6342.  
  6343.         Thesin_addr field of the sockaddr_in structure specifies a local or
  6344.         remote IP address.  Each network interface has its own unique IP
  6345.         address.   The special value INADDR_ANY may be used in this field to
  6346.         effect "wildcard" matching.  Given in a bind() call, this value
  6347.         leaves the local IP address of the socket unspecified, so that the
  6348.         socket will receive connections or messages directed at any of the
  6349.         valid IP addresses of the system.  This can prove useful when a
  6350.         process neither knows nor cares what the local IP address is or when
  6351.         a process wishes to receive requests using all of its network
  6352.         interfaces.   Thesockaddr_in structure given in the bind() call must
  6353.         specify an in_addr value of either IPADDR_ANY or one of the system's
  6354.         valid IP addresses.  Requests to bind any other address will elicit
  6355.         theerror EADDRNOTAVAIL. When a connect() call is made for a socket
  6356.         that has a wildcard local address, the system sets the sin_addr
  6357.         field of the socket to the IP address of the network interface that
  6358.         thepackets for that connection are routed via.
  6359.  
  6360.  
  6361.         Thesin_port field of the sockaddr_in structure specifies a port
  6362.         number used by TCP or UDP. The local port address specified in a
  6363.         bind() call is restricted to be greater than IPPORT_RESERVED
  6364.         (defined in <netinet/in.h>) unless the creating process is running
  6365.         asthe super-user, providing a space of protected port numbers.  In
  6366.         addition, the local port address must not be in use by any socket of
  6367.         same address family and type.  Requests to bind sockets to port
  6368.         numbers being used by other sockets return the error EADDRINUSE.  If
  6369.         thelocal port address is specified as 0, then the system picks a
  6370.         unique port address greater than IPPORT_RESERVED.  A unique local
  6371.         port address is also picked when a socket which is not bound is used
  6372.         ina connect() or send() call.  This allows programs which do not
  6373.         care which local port number is used to set up TCP connections by
  6374.         sim- ply calling socket() and then connect(), and to send UDP
  6375.         datagrams with a socket() call followed by a send() call.
  6376.  
  6377.  
  6378.         Although this implementation restricts sockets to unique local port
  6379.         numbers, TCP allows multiple simultaneous connections involving the
  6380.         same local port number so long as the remote IP addresses or port
  6381.         numbers are different for each connection.  Programs may explicitly
  6382.         override the socket restriction by setting the SO_REUSEADDR socket
  6383.         option with setsockopt (see getsockopt()).
  6384.  
  6385.  
  6386.    SEE ALSO
  6387.         bsdsocket.library/bind(), bsdsocket.library/connect(),
  6388.  
  6389.  
  6390. System Manual                  AmiTCP/IP                 Section D.1    195
  6391.  
  6392.  
  6393.  
  6394.         bsdsocket.library/getsockopt(), bsdsocket.library/IoctlSocket(),
  6395.         bsdsocket.library/send(), bsdsocket.library/socket(),
  6396.         bsdsocket.library/gethostent(), bsdsocket.library/getnetent(),
  6397.         bsdsocket.library/getprotoent(), bsdsocket.library/getservent(),
  6398.         bsdsocket.library/inet_addr(), arp, icmp, ip, tcp, udp
  6399.  
  6400.  
  6401.         Network Information Center, DDN Protocol Handbook (3 vols.),
  6402.         Network  Information  Center, SRI International, Menlo Park,
  6403.         Calif.,  1985.
  6404.         A AmiTCP/IP Interprocess Communication Primer
  6405.  
  6406.  
  6407.    WARNING
  6408.         TheInternet protocol support is subject to change as the Internet
  6409.         protocols develop.  Users should not depend on details of the
  6410.         current implementation, but rather the services exported.
  6411.  
  6412.  
  6413. 196    Section D.1                 AmiTCP/IP                  System Manual
  6414.  
  6415.  
  6416.  
  6417. D.1.5   ip
  6418.  
  6419.  
  6420.    NAME
  6421.         ip- Internet Protocol
  6422.  
  6423.  
  6424.    SYNOPSIS
  6425.         #include <sys/socket.h>
  6426.         #include <netinet/in.h>
  6427.  
  6428.  
  6429.         int
  6430.         socket(AF_INET, SOCK_RAW, proto)
  6431.  
  6432.  
  6433.    DESCRIPTION
  6434.         IPis the transport layer protocol used by the Internet protocol
  6435.         family.   Optionsmay be set at the IP level when using higher-level
  6436.         protocols that are based on IP (such as TCP and UDP). It may also be
  6437.         accessed through a ``raw socket'' when developing new protocols, or
  6438.         special purpose applica- tions.
  6439.  
  6440.  
  6441.         A single generic option is supported at the IP level, IP_OPTIONS,
  6442.         that may be used to provide IP options to be transmitted in the IP
  6443.         header of each outgoing packet.  Options are set with setsockopt()
  6444.         andexamined with getsockopt().  The format of IP options to be sent
  6445.         isthat specified by the IP protocol specification, with one
  6446.         exception: the list of addresses for Source Route options must
  6447.         include the first-hop gateway at the beginning of the list of
  6448.         gateways.   The first-hop gateway address will be extracted from the
  6449.         option list and the size adjusted accordingly before use.  IP
  6450.         options may be used with any socket type in the Internet family.
  6451.  
  6452.  
  6453.         RawIP sockets are connectionless, and are normally used with the
  6454.         sendto and recvfrom calls, though the connect() call may also be
  6455.         used to fix the destination for future packets (in which case the
  6456.         recv() and send() system calls may be used).
  6457.  
  6458.  
  6459.         Ifproto is 0, the default protocol IPPROTO_RAW is used for outgoing
  6460.         packets, and only incoming packets destined for that protocol are
  6461.         received.   If proto is non-zero, that protocol number will be used
  6462.         onoutgoing packets and to filter incoming packets.
  6463.  
  6464.  
  6465.         Outgoing packets automatically have an IP header prepended to them
  6466.         (based on the destination address and the protocol number the socket
  6467.         iscreated with).  Incoming packets are received with IP header and
  6468.         options  intact.
  6469.  
  6470.  
  6471.    DIAGNOSTICS
  6472.         A socket operation may fail with one of the following errors
  6473.         returned:
  6474.  
  6475.  
  6476.         [EISCONN]         when trying to establish a connection ona socket
  6477.                           which already has one, orwhen trying to send a
  6478.                           datagram with the destination address specified and
  6479.                           the socket is already connected;
  6480.  
  6481.  
  6482.         [ENOTCONN]        when trying tosend a datagram, but no destination
  6483.                           address is specified, andthe socket hasn't been
  6484.  
  6485.  
  6486. System Manual                  AmiTCP/IP                 Section D.1    197
  6487.  
  6488.  
  6489.  
  6490.                           connected;
  6491.  
  6492.  
  6493.         [ENOBUFS]         when the system runs out of memory for aninternal
  6494.                           data structure;
  6495.  
  6496.  
  6497.         [EADDRNOTAVAIL]  when an attempt is made to create a socket with a
  6498.                           network address for whichno network interface
  6499.                           exists.
  6500.  
  6501.  
  6502.         Thefollowing errors specific to IP may occur when setting or
  6503.         getting IP options:
  6504.  
  6505.  
  6506.         [EINVAL]          An unknown socket option name was given.
  6507.  
  6508.  
  6509.         [EINVAL]          The IP option field was improperly formed; an
  6510.                           option field was shorterthan the minimum value or
  6511.                           longer than the option buffer provided.
  6512.  
  6513.  
  6514.    SEE ALSO
  6515.         bsdsocket.library/getsockopt(), bsdsocket.library/send(),
  6516.         bsdsocket.library/recv(), icmp, inet
  6517.  
  6518.  
  6519.    HISTORY
  6520.         Theip protocol appeared in 4.2BSD.
  6521.  
  6522.  
  6523. 198    Section D.1                 AmiTCP/IP                  System Manual
  6524.  
  6525.  
  6526.  
  6527. D.1.6   lo
  6528.  
  6529.    NAME
  6530.         lo- Software Loopback Network Interface
  6531.  
  6532.  
  6533.    SYNOPSIS
  6534.         pseudo-device
  6535.         loop
  6536.  
  6537.  
  6538.    DESCRIPTION
  6539.         Theloop interface is a software loopback mechanism which may be
  6540.         used for performance analysis, software testing, and/or local
  6541.         communication.  There is no SANA-II interface associated with lo.
  6542.         Aswith other network interfaces, the loopback interface must have
  6543.         network addresses assigned for each address family with which it is
  6544.         tobe used.   These addresses may be set or changed with the
  6545.         SIOCSIFADDR ioctl. The loopback interface should be the last
  6546.         interface configured, as protocols may use the order of
  6547.         configuration as an indication of priority.  The loopback should
  6548.         never be configured first unless no hardware interfaces exist.
  6549.  
  6550.  
  6551.    DIAGNOSTICS
  6552.         "lo%d: can't handle af%d."
  6553.         Theinterface was handed a message with ad- dresses formatted in an
  6554.         unsuitable address family; the packet was dropped.
  6555.  
  6556.  
  6557.    SEE ALSO
  6558.         inet, if, netutil/ifconfig
  6559.  
  6560.  
  6561.    BUGS
  6562.         Older BSD Unix systems enabled the loopback interface
  6563.         automatically, using a nonstandard Internet address (127.1).  Use
  6564.         ofthat address is now discouraged; a reserved host address for the
  6565.         local network should be used instead.
  6566.  
  6567.  
  6568. System Manual                  AmiTCP/IP                 Section D.1    199
  6569.  
  6570.  
  6571.  
  6572. D.1.7   routing
  6573.  
  6574.  
  6575.    NAME
  6576.         routing - system supporting for local network packet routing
  6577.  
  6578.  
  6579.    DESCRIPTION
  6580.         Thenetwork facilities provided general packet routing,
  6581.         leaving routing table maintenance to applications processes.
  6582.  
  6583.  
  6584.         A simple set of data structures comprise a ``routing table''
  6585.         used in selecting the appropriate network interface when
  6586.         transmitting packets.  This table contains a single entry for
  6587.         each route to a specific network or host.  A user process, the
  6588.         routing daemon, maintains this data base with the aid of two
  6589.         socket specific ioctl commands, SIOCADDRT and SIOCDELRT.
  6590.         Thecommands allow the addition and deletion of a single
  6591.         routing table entry, respectively.  Routing table
  6592.         manipulations may only be carried out by super-user.
  6593.  
  6594.  
  6595.         A routing table entry has the following form, as defined  in
  6596.         <net/route.h>:
  6597.              struct rtentry -
  6598.                   u_long     rt_hash;
  6599.                   struct     sockaddr rt_dst;
  6600.                   struct     sockaddr rt_gateway;
  6601.                   short      rt_flags;
  6602.                   short      rt_refcnt;
  6603.                   u_long     rt_use;
  6604.                   struct     ifnet  *rt_ifp;
  6605.              ";
  6606.         with rt_flags defined from:
  6607.           #define   RTF_UP          0x1               /* route usable */
  6608.           #define   RTF_GATEWAY    0x2  /* destination is a gateway */
  6609.           #define   RTF_HOST  0x4     /* host entry (net otherwise) */
  6610.  
  6611.  
  6612.         Routing table entries come in three flavors: for a specific
  6613.         host, for all hosts on a specific network, for any destination
  6614.         notmatched by entries of the first two types (a wildcard
  6615.         route).   When the system is booted, each network interface
  6616.         autoconfigured installs a routing table entry when it wishes
  6617.         tohave packets sent through it.  Normally the interface
  6618.         specifies the route through it is a ``direct'' connection to
  6619.         thedestination host or network.  If the route is direct, the
  6620.         transport layer of a protocol family usually requests the
  6621.         packet be sent to the same host specified in the packet.
  6622.         Otherwise, the interface may be requested to address the
  6623.         packet to an entity different from the eventual recipient
  6624.         (that is, the packet is forwarded).
  6625.  
  6626.  
  6627.         Routing table entries installed by a user process may not
  6628.         specify the hash, reference count, use, or interface fields;
  6629.         these are filled in by the routing routines.  If a route is in
  6630.         usewhen it is deleted (rt_refcnt is non-zero), the resources
  6631.         associated with it will not be reclaimed until all references
  6632.         toit are removed.
  6633.  
  6634.  
  6635. 200    Section D.1                 AmiTCP/IP                  System Manual
  6636.  
  6637.  
  6638.  
  6639.         Therouting code returns EEXIST if requested to duplicate an
  6640.         existing entry, ESRCH if requested to delete a non-existent
  6641.         entry, or ENOBUFS if insufficient resources were available to
  6642.         install a new route.
  6643.  
  6644.  
  6645.         Thert_use field contains the number of packets sent along the
  6646.         route.   This value is used to select among multiple routes to
  6647.         thesame destination.  When multiple routes to the same
  6648.         destination exist, the least used route is selected.
  6649.  
  6650.  
  6651.         A wildcard routing entry is specified with a zero destination
  6652.         address value.  Wildcard routes are used only when the system
  6653.         fails to find a route to the destination host and network.
  6654.         Thecombination of wildcard routes and routing redirects can
  6655.         provide an economical mechanism for routing traffic.
  6656.  
  6657.  
  6658.    SEE ALSO
  6659.         bsdsocket.library/IoctlSocket(), netutil/route
  6660.  
  6661.  
  6662. System Manual                  AmiTCP/IP                 Section D.1    201
  6663.  
  6664.  
  6665.  
  6666. D.1.8   tcp
  6667.  
  6668.  
  6669.    NAME
  6670.         tcp- Internet Transmission Control Protocol
  6671.  
  6672.  
  6673.    SYNOPSIS
  6674.         #include <sys/socket.h>
  6675.         #include <netinet/in.h>
  6676.  
  6677.  
  6678.         int
  6679.         socket(AF_INET, SOCK_STREAM, 0)
  6680.  
  6681.  
  6682.    DESCRIPTION
  6683.         TheTCP protocol provides reliable, flow-controlled, two-way
  6684.         transmission of data.  It is a byte-stream protocol used to support
  6685.         theSOCK_STREAM abstraction.  TCP uses the standard Internet address
  6686.         format and, in addition, provides a per-host collection of ``port
  6687.         addresses''. Thus, each address is composed of an Internet address
  6688.         specifying the host and network, with a specific TCP port on the
  6689.         host identifying the peer entity.
  6690.  
  6691.  
  6692.         Sockets utilizing the tcp protocol are either ``active'' or
  6693.         ``passive''.   Active sockets initiate connections to passive
  6694.         sockets.   By default TCP sockets are created active; to create a
  6695.         passive socket the listen() bsdsocket.library function call must be
  6696.         used after binding the socket with the bind() bsdsocket.library
  6697.         function call.  Only passive sockets may use the accept() call to
  6698.         accept incoming connections.  Only active sockets may use the
  6699.         connect() call to initiate connections.
  6700.  
  6701.  
  6702.         Passive sockets may ``underspecify'' their location to match
  6703.         incoming connection requests from multiple networks.  This
  6704.         technique, termed ``wildcard addressing'', allows a single server to
  6705.         provide service to clients on multiple networks.  To create a socket
  6706.         which listens on all networks, the Internet address INADDR_ANY must
  6707.         bebound.   The TCP port may still be specified at this time; if the
  6708.         port is not specified the bsdsocket.library function will assign
  6709.         one.   Once a connection has been established the socket's address is
  6710.         fixed by the peer entity's location.  The address assigned the
  6711.         socket is the address associated with the network interface through
  6712.         which packets are being transmitted and received.  Normally this
  6713.         address corresponds to the peer entity's network.
  6714.  
  6715.  
  6716.         TCPsupports one socket option which is set with setsockopt() and
  6717.         tested with getsockopt().  Under most circumstances, TCP sends data
  6718.         when it is presented; when outstanding data has not yet been
  6719.         acknowledged, it gathers small amounts of output to be sent in a
  6720.         single packet once an acknowledgement is received.  For a small
  6721.         number of clients, such as X Window System functions that
  6722.         send a stream of mouse events which receive no replies, this
  6723.         packetization may cause significant delays.  Therefore, TCP provides
  6724.         a boolean option, TCP_NODELAY (from <netinet/tcp.h>, to defeat this
  6725.         algorithm.   Theoption level for the setsockopt call is the protocol
  6726.         number for TCP, available from getprotobyname().
  6727.  
  6728.  
  6729.         Options at the IP transport level may be used with TCP; SEE ALSO ip.
  6730.  
  6731.  
  6732. 202    Section D.1                 AmiTCP/IP                  System Manual
  6733.  
  6734.  
  6735.  
  6736.         Incoming connection requests that are source-routed are noted, and
  6737.         thereverse source route is used in responding.
  6738.  
  6739.  
  6740.    DIAGNOSTICS
  6741.         A socket operation may fail with one of the following errors
  6742.         returned:
  6743.  
  6744.  
  6745.         [EISCONN]         when trying to establish a connection ona socket
  6746.                           which already has one;
  6747.  
  6748.  
  6749.         [ENOBUFS]         when the AmiTCP/IP runs out of memory foran internal
  6750.                           data structure;
  6751.  
  6752.  
  6753.         [ETIMEDOUT]       when a connection was dropped due to excessive
  6754.                           retransmissions;
  6755.  
  6756.  
  6757.         [ECONNRESET]      when the remote peerforces the connection to be
  6758.                           closed;
  6759.  
  6760.  
  6761.         [ECONNREFUSED]    when the remote peer actively refuses connection
  6762.                           establishment (usually because no process is
  6763.                           listening to the port);
  6764.  
  6765.  
  6766.         [EADDRINUSE]      when an attempt is made to create a socket with a
  6767.                           port which has already been allocated;
  6768.  
  6769.  
  6770.         [EADDRNOTAVAIL]  when an attempt is made to create a socket with a
  6771.                           network address for whichno network interface
  6772.                           exists.
  6773.  
  6774.  
  6775.    SEE ALSO
  6776.         bsdsocket.library/getsockopt(), bsdsocket.library/socket(),
  6777.         bsdsocket.library/bind(), bsdsocket.library/listen(),
  6778.         bsdsocket.library/accept(), bsdsocket.library/connect(), inet,
  6779.         ip,<sys/socket.h>, <netinet/tcp.h>, <netinet/in.h>
  6780.  
  6781.  
  6782.    HISTORY
  6783.         Thetcp protocol stack appeared in 4.2BSD.
  6784.  
  6785.  
  6786. System Manual                  AmiTCP/IP                 Section D.1    203
  6787.  
  6788.  
  6789.  
  6790. D.1.9   udp
  6791.  
  6792.  
  6793.    NAME
  6794.         udp- Internet User Datagram Protocol
  6795.  
  6796.  
  6797.    SYNOPSIS
  6798.         #include <sys/socket.h>
  6799.         #include <netinet/in.h>
  6800.  
  6801.  
  6802.         int
  6803.         socket(AF_INET, SOCK_DGRAM, 0)
  6804.  
  6805.  
  6806.    DESCRIPTION
  6807.         UDPis a simple, unreliable datagram protocol which is used to
  6808.         support the SOCK_DGRAM abstraction for the Internet protocol family.
  6809.         UDPsockets are connectionless, and are normally used with the
  6810.         sendto() and recvfrom() calls, though the connect() call may also be
  6811.         used to fix the destination for future packets (in which case the
  6812.         recv() and send() function calls may be used).
  6813.  
  6814.  
  6815.         UDPaddress formats are identical to those used by TCP. In
  6816.         particular UDP provides a port identifier in addition to the normal
  6817.         Internet address format.  Note that the UDP port space is separate
  6818.         from the TCP port space (i.e. a UDP port may not be ``connected'' to
  6819.         a TCP port). In addition broadcast packets may be sent (assuming the
  6820.         underlying network supports this) by using a reserved ``broadcast
  6821.         address''; this address is network interface dependent.
  6822.  
  6823.  
  6824.         Options at the IP transport level may be used with UDP; SEE ALSO ip.
  6825.  
  6826.  
  6827.    DIAGNOSTICS
  6828.         A socket operation may fail with one of the following errors
  6829.         returned:
  6830.  
  6831.  
  6832.         [EISCONN]         when trying to establish a connection ona socket
  6833.                           which already has one, orwhen trying to send a
  6834.                           datagram with the destination address specified and
  6835.                           the socket is already connected;
  6836.  
  6837.  
  6838.         [ENOTCONN]        when trying tosend a datagram, but no destination
  6839.                           address is specified, andthe socket hasn't been
  6840.                           connected;
  6841.  
  6842.  
  6843.         [ENOBUFS]         when the system runs out of memory for an
  6844.                           internal data structure;
  6845.  
  6846.  
  6847.         [EADDRINUSE]      when an attempt is made to create a socket with a
  6848.                           port which has already been allocated;
  6849.  
  6850.  
  6851.         [EADDRNOTAVAIL]  when an attempt is made to create a socket with a
  6852.                           network address for whichno network interface
  6853.                           exists.
  6854.  
  6855.  
  6856.    SEE ALSO
  6857.         bsdsocket.library/getsockopt(), bsdsocket.library/recv(),
  6858.         bsdsocket.library/send(), bsdsocket.library/socket(), inet, ip
  6859.  
  6860.  
  6861. 204    Section D.1                 AmiTCP/IP                  System Manual
  6862.  
  6863.  
  6864.  
  6865.    HISTORY
  6866.         Theudp protocol appeared in 4.2BSD.
  6867.